-- NOTE: This file is auto generated by ./sql-generator

-- AlbumRepository.getById
select
  "album".*,
  (
    select
      to_json(obj)
    from
      (
        select
          "id",
          "name",
          "email",
          "avatarColor",
          "profileImagePath",
          "profileChangedAt"
        from
          "user"
        where
          "user"."id" = "album"."ownerId"
      ) as obj
  ) as "owner",
  coalesce(
    (
      select
        "album_user_metadata"."isFavorite"
      from
        "album_user_metadata"
      where
        "album_user_metadata"."albumId" = "album"."id"
        and "album_user_metadata"."userId" = $1
    ),
    false
  ) as "isFavorite",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "album_user"."role",
          (
            select
              to_json(obj)
            from
              (
                select
                  "id",
                  "name",
                  "email",
                  "avatarColor",
                  "profileImagePath",
                  "profileChangedAt"
                from
                  "user"
                where
                  "user"."id" = "album_user"."userId"
              ) as obj
          ) as "user"
        from
          "album_user"
        where
          "album_user"."albumId" = "album"."id"
      ) as agg
  ) as "albumUsers",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "shared_link".*
        from
          "shared_link"
        where
          "shared_link"."albumId" = "album"."id"
      ) as agg
  ) as "sharedLinks",
  (
    select
      json_agg("asset") as "assets"
    from
      (
        select
          "asset".*,
          "asset_exif" as "exifInfo"
        from
          "asset"
          left join "asset_exif" on "asset"."id" = "asset_exif"."assetId"
          inner join "album_asset" on "album_asset"."assetId" = "asset"."id"
        where
          "album_asset"."albumId" = "album"."id"
          and "asset"."deletedAt" is null
          and "asset"."visibility" in ('archive', 'timeline')
        order by
          "asset"."fileCreatedAt" desc
      ) as "asset"
  ) as "assets"
from
  "album"
where
  "album"."id" = $2
  and "album"."deletedAt" is null

-- AlbumRepository.getByAssetId
select
  "album".*,
  coalesce(
    (
      select
        "album_user_metadata"."isFavorite"
      from
        "album_user_metadata"
      where
        "album_user_metadata"."albumId" = "album"."id"
        and "album_user_metadata"."userId" = $1
    ),
    false
  ) as "isFavorite",
  (
    select
      to_json(obj)
    from
      (
        select
          "id",
          "name",
          "email",
          "avatarColor",
          "profileImagePath",
          "profileChangedAt"
        from
          "user"
        where
          "user"."id" = "album"."ownerId"
      ) as obj
  ) as "owner",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "album_user"."role",
          (
            select
              to_json(obj)
            from
              (
                select
                  "id",
                  "name",
                  "email",
                  "avatarColor",
                  "profileImagePath",
                  "profileChangedAt"
                from
                  "user"
                where
                  "user"."id" = "album_user"."userId"
              ) as obj
          ) as "user"
        from
          "album_user"
        where
          "album_user"."albumId" = "album"."id"
      ) as agg
  ) as "albumUsers"
from
  "album"
  inner join "album_asset" on "album_asset"."albumId" = "album"."id"
where
  (
    "album"."ownerId" = $2
    or exists (
      select
      from
        "album_user"
      where
        "album_user"."albumId" = "album"."id"
        and "album_user"."userId" = $3
    )
  )
  and "album_asset"."assetId" = $4
  and "album"."deletedAt" is null
order by
  "album"."createdAt" desc,
  "album"."createdAt" desc

-- AlbumRepository.getByAssetIds
select
  "album"."id",
  "album_asset"."assetId"
from
  "album"
  inner join "album_asset" on "album_asset"."albumId" = "album"."id"
where
  (
    "album"."ownerId" = $1
    or exists (
      select
      from
        "album_user"
      where
        "album_user"."albumId" = "album"."id"
        and "album_user"."userId" = $2
    )
  )
  and "album_asset"."assetId" in ($3)
  and "album"."deletedAt" is null

-- AlbumRepository.getMetadataForIds
select
  "album_asset"."albumId" as "albumId",
  min(
    ("asset"."localDateTime" AT TIME ZONE 'UTC'::text)::date
  ) as "startDate",
  max(
    ("asset"."localDateTime" AT TIME ZONE 'UTC'::text)::date
  ) as "endDate",
  max("asset"."updatedAt") as "lastModifiedAssetTimestamp",
  count("asset"."id")::int as "assetCount"
from
  "asset"
  inner join "album_asset" on "album_asset"."assetId" = "asset"."id"
where
  "asset"."visibility" in ('archive', 'timeline')
  and "album_asset"."albumId" in ($1)
  and "asset"."deletedAt" is null
group by
  "album_asset"."albumId"

-- AlbumRepository.getOwned
select
  "album".*,
  coalesce(
    (
      select
        "album_user_metadata"."isFavorite"
      from
        "album_user_metadata"
      where
        "album_user_metadata"."albumId" = "album"."id"
        and "album_user_metadata"."userId" = $1
    ),
    false
  ) as "isFavorite",
  (
    select
      to_json(obj)
    from
      (
        select
          "id",
          "name",
          "email",
          "avatarColor",
          "profileImagePath",
          "profileChangedAt"
        from
          "user"
        where
          "user"."id" = "album"."ownerId"
      ) as obj
  ) as "owner",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "album_user"."role",
          (
            select
              to_json(obj)
            from
              (
                select
                  "id",
                  "name",
                  "email",
                  "avatarColor",
                  "profileImagePath",
                  "profileChangedAt"
                from
                  "user"
                where
                  "user"."id" = "album_user"."userId"
              ) as obj
          ) as "user"
        from
          "album_user"
        where
          "album_user"."albumId" = "album"."id"
      ) as agg
  ) as "albumUsers",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "shared_link".*
        from
          "shared_link"
        where
          "shared_link"."albumId" = "album"."id"
      ) as agg
  ) as "sharedLinks"
from
  "album"
where
  "album"."ownerId" = $2
  and "album"."deletedAt" is null
order by
  "album"."createdAt" desc

-- AlbumRepository.getShared
select
  "album".*,
  coalesce(
    (
      select
        "album_user_metadata"."isFavorite"
      from
        "album_user_metadata"
      where
        "album_user_metadata"."albumId" = "album"."id"
        and "album_user_metadata"."userId" = $1
    ),
    false
  ) as "isFavorite",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "album_user"."role",
          (
            select
              to_json(obj)
            from
              (
                select
                  "id",
                  "name",
                  "email",
                  "avatarColor",
                  "profileImagePath",
                  "profileChangedAt"
                from
                  "user"
                where
                  "user"."id" = "album_user"."userId"
              ) as obj
          ) as "user"
        from
          "album_user"
        where
          "album_user"."albumId" = "album"."id"
      ) as agg
  ) as "albumUsers",
  (
    select
      to_json(obj)
    from
      (
        select
          "id",
          "name",
          "email",
          "avatarColor",
          "profileImagePath",
          "profileChangedAt"
        from
          "user"
        where
          "user"."id" = "album"."ownerId"
      ) as obj
  ) as "owner",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "shared_link".*
        from
          "shared_link"
        where
          "shared_link"."albumId" = "album"."id"
      ) as agg
  ) as "sharedLinks"
from
  "album"
where
  (
    exists (
      select
      from
        "album_user"
      where
        "album_user"."albumId" = "album"."id"
        and (
          "album"."ownerId" = $2
          or "album_user"."userId" = $3
        )
    )
    or exists (
      select
      from
        "shared_link"
      where
        "shared_link"."albumId" = "album"."id"
        and "shared_link"."userId" = $4
    )
  )
  and "album"."deletedAt" is null
order by
  "album"."createdAt" desc

-- AlbumRepository.getNotShared
select
  "album".*,
  coalesce(
    (
      select
        "album_user_metadata"."isFavorite"
      from
        "album_user_metadata"
      where
        "album_user_metadata"."albumId" = "album"."id"
        and "album_user_metadata"."userId" = $1
    ),
    false
  ) as "isFavorite",
  (
    select
      to_json(obj)
    from
      (
        select
          "id",
          "name",
          "email",
          "avatarColor",
          "profileImagePath",
          "profileChangedAt"
        from
          "user"
        where
          "user"."id" = "album"."ownerId"
      ) as obj
  ) as "owner"
from
  "album"
where
  "album"."ownerId" = $2
  and "album"."deletedAt" is null
  and not exists (
    select
    from
      "album_user"
    where
      "album_user"."albumId" = "album"."id"
  )
  and not exists (
    select
    from
      "shared_link"
    where
      "shared_link"."albumId" = "album"."id"
  )
order by
  "album"."createdAt" desc

-- AlbumRepository.removeAssetsFromAll
delete from "album_asset"
where
  "album_asset"."assetId" in ($1)

-- AlbumRepository.getAssetIds
select
  *
from
  "album_asset"
where
  "album_asset"."albumId" = $1
  and "album_asset"."assetId" in ($2)

-- AlbumRepository.getContributorCounts
select
  "asset"."ownerId" as "userId",
  count(*) as "assetCount"
from
  "album_asset"
  inner join "asset" on "asset"."id" = "assetId"
where
  "asset"."deletedAt" is null
  and "album_asset"."albumId" = $1
group by
  "asset"."ownerId"
order by
  "assetCount" desc

-- AlbumRepository.copyAlbums
insert into
  "album_asset"
select
  "album_asset"."albumId",
  $1 as "assetId"
from
  "album_asset"
where
  "album_asset"."assetId" = $2
on conflict do nothing
