// SPDX-FileCopyrightText: 2025-2026 Uncore <https://github.com/uncor3>
// SPDX-License-Identifier: AGPL-3.0-or-later

pub const RECENTS_ALBUM_ID: i32 = -1;
pub const FAVS_ALBUM_ID: i32 = -2;
pub const RECENTLY_DELETED_ALBUM_ID: i32 = -3;
pub const HIDDEN_ALBUM_ID: i32 = -4;

pub static IOS_15_ALBUM_QUERY_STATEMENT: &str = "SELECT
                ZGENERICALBUM.Z_PK,
                ZGENERICALBUM.ZTITLE,
                ZGENERICALBUM.ZCACHEDCOUNT,
                ZASSET.ZDIRECTORY,
                ZASSET.ZFILENAME
            FROM ZGENERICALBUM
            LEFT JOIN ZASSET ON ZGENERICALBUM.ZKEYASSET = ZASSET.Z_PK
            WHERE ZGENERICALBUM.ZKEYASSET IS NOT NULL
        ";

// pub static IOS_26_ALBUM_QUERY_STATEMENT: &str = "SELECT
//                             ZGENERICALBUM.Z_PK,
//                             ZGENERICALBUM.ZTITLE,
//                             ZGENERICALBUM.ZCACHEDCOUNT,
//                             ZASSET.ZDIRECTORY,
//                             ZASSET.ZFILENAME
//                         FROM ZGENERICALBUM
//                         LEFT JOIN ZASSET ON ZGENERICALBUM.Z_ENT = ZASSET.Z_PK
//                         WHERE ZGENERICALBUM.Z_ENT IS NOT NULL
//                         AND ZGENERICALBUM.ZTITLE IS NOT NULL
//                         AND ZGENERICALBUM.ZCACHEDCOUNT IS NOT 0
//                     ";

pub static IOS_26_ALBUM_QUERY_STATEMENT: &str = "
    SELECT
        ZGENERICALBUM.Z_PK,
        ZGENERICALBUM.ZTITLE,
        ZGENERICALBUM.ZCACHEDCOUNT,
        COALESCE(ZASSET.ZDIRECTORY, ''),
        COALESCE(ZASSET.ZFILENAME, '')
    FROM ZGENERICALBUM
    LEFT JOIN ZASSET
        ON ZASSET.Z_PK = COALESCE(
            (
                SELECT ZASSET.Z_PK
                FROM ZASSET
                WHERE ZASSET.Z_PK = ZGENERICALBUM.ZCUSTOMKEYASSET
                AND ZASSET.ZDIRECTORY IS NOT NULL
                AND ZASSET.ZFILENAME IS NOT NULL
                LIMIT 1
            ),
            (
                SELECT {table}.Z_3ASSETS
                FROM {table}
                JOIN ZASSET
                    ON ZASSET.Z_PK = {table}.Z_3ASSETS
                WHERE {table}.{album} = ZGENERICALBUM.Z_PK
                AND ZASSET.ZDIRECTORY IS NOT NULL
                AND ZASSET.ZFILENAME IS NOT NULL
                ORDER BY {table}.Z_FOK_3ASSETS DESC
                LIMIT 1
            )
        )
    WHERE ZGENERICALBUM.ZTITLE IS NOT NULL
    AND ZGENERICALBUM.ZCACHEDCOUNT > 0;
";

pub static RECENTS_ALBUM_QUERY: &str = "
    SELECT
    ZASSET.ZFILENAME,
    ZASSET.ZDIRECTORY,
    (
        SELECT COUNT(*)
        FROM ZASSET
        WHERE ZASSET.ZTRASHEDSTATE = 0
        AND ZASSET.ZVISIBILITYSTATE = 0
        AND ZASSET.ZHIDDEN = 0
    )
    FROM ZASSET
    WHERE ZASSET.ZTRASHEDSTATE = 0
    AND ZASSET.ZVISIBILITYSTATE = 0
    AND ZASSET.ZHIDDEN = 0
    ORDER BY ZASSET.Z_PK DESC
    LIMIT 1
";

pub static RECENTS_QUERY: &str = "
    SELECT
    ZASSET.ZFILENAME,
    ZASSET.ZDIRECTORY
    FROM ZASSET
    WHERE ZASSET.ZTRASHEDSTATE = 0
    AND ZASSET.ZVISIBILITYSTATE = 0
    AND ZASSET.ZHIDDEN = 0
    ORDER BY ZASSET.Z_PK DESC
";

pub static FAVS_ALBUM_QUERY: &str = "
    SELECT
        ZASSET.ZFILENAME,
        ZASSET.ZDIRECTORY,
        (
            SELECT COUNT(*)
            FROM ZASSET
            WHERE ZASSET.ZFAVORITE = 1
            AND ZASSET.ZTRASHEDSTATE = 0
            AND ZASSET.ZVISIBILITYSTATE = 0
            AND ZASSET.ZHIDDEN = 0
        )
    FROM ZASSET
    WHERE ZASSET.ZFAVORITE = 1
    AND ZASSET.ZTRASHEDSTATE = 0
    AND ZASSET.ZVISIBILITYSTATE = 0
    AND ZASSET.ZHIDDEN = 0
    ORDER BY ZASSET.Z_PK DESC
    LIMIT 1
";

pub static FAVS_QUERY: &str = "
    SELECT
        ZASSET.ZFILENAME,
        ZASSET.ZDIRECTORY
    FROM ZASSET
    WHERE ZASSET.ZFAVORITE = 1
    AND ZASSET.ZTRASHEDSTATE = 0
    AND ZASSET.ZVISIBILITYSTATE = 0
    AND ZASSET.ZHIDDEN = 0
    ORDER BY ZASSET.Z_PK DESC
";

pub static RECENTLY_DELETED_ALBUM_QUERY: &str = "
    SELECT
        ZASSET.ZFILENAME,
        ZASSET.ZDIRECTORY,
        (SELECT COUNT(*) FROM ZASSET WHERE ZASSET.ZTRASHEDSTATE = 1)
    FROM ZASSET
    WHERE ZASSET.ZTRASHEDSTATE = 1
    ORDER BY ZASSET.Z_PK DESC
    LIMIT 1
";

pub static RECENTLY_DELETED_QUERY: &str = "
    SELECT
        ZASSET.ZFILENAME,
        ZASSET.ZDIRECTORY
    FROM ZASSET
    WHERE ZASSET.ZTRASHEDSTATE = 1
    ORDER BY ZASSET.Z_PK DESC
";

pub static HIDDEN_ALBUM_QUERY: &str = "
    SELECT
        ZASSET.ZFILENAME,
        ZASSET.ZDIRECTORY,
        (
            SELECT COUNT(*)
            FROM ZASSET
            WHERE ZASSET.ZTRASHEDSTATE = 0
            AND (ZASSET.ZVISIBILITYSTATE = 1 OR ZASSET.ZHIDDEN = 1)
        )
    FROM ZASSET
    WHERE ZASSET.ZTRASHEDSTATE = 0
    AND (ZASSET.ZVISIBILITYSTATE = 1 OR ZASSET.ZHIDDEN = 1)
    ORDER BY ZASSET.Z_PK DESC
    LIMIT 1
";

pub static HIDDEN_QUERY: &str = "
    SELECT
        ZASSET.ZFILENAME,
        ZASSET.ZDIRECTORY
    FROM ZASSET
    WHERE ZASSET.ZTRASHEDSTATE = 0
    AND (ZASSET.ZVISIBILITYSTATE = 1 OR ZASSET.ZHIDDEN = 1)
    ORDER BY ZASSET.Z_PK DESC
";

//FIXME: is Z_3ASSETS consistent ?
pub static ALBUM_CONTENTS_QUERY_TEMPLATE: &str = r#"
    SELECT
        ZASSET.ZDIRECTORY,
        ZASSET.ZFILENAME
    FROM ZGENERICALBUM
    LEFT JOIN {table} ON ZGENERICALBUM.Z_PK = {table}.{album}
    LEFT JOIN ZASSET ON {table}.Z_3ASSETS = ZASSET.Z_PK
    WHERE ZGENERICALBUM.Z_PK = ?
    AND ZASSET.ZTRASHEDSTATE = 0
    AND ZASSET.ZVISIBILITYSTATE = 0
    AND ZASSET.ZHIDDEN = 0
"#;

pub static GALLERY_TOTAL_SIZE_QUERY: &str =
    "SELECT COALESCE(SUM(ZORIGINALFILESIZE), 0) FROM ZADDITIONALASSETATTRIBUTES";

pub static FS_GALLERY_PROVIDER_NAME: &str = "FS";
pub static SQLITE_GALLERY_PROVIDER_NAME: &str = "SQLite";
pub static SQLITE_VFS_GALLERY_PROVIDER_NAME: &str = "SQLite VFS";
pub static PHOTOS_SQLITE_REMOTE_PATH: &str = "/PhotoData/Photos.sqlite";
pub static PHOTOS_SQLITE_SHM_REMOTE_PATH: &str = "/PhotoData/Photos.sqlite-shm";
pub static PHOTOS_SQLITE_WAL_REMOTE_PATH: &str = "/PhotoData/Photos.sqlite-wal";
pub static DCIM_REMOTE_PATH: &str = "/DCIM";
