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

-- AlbumRepository.getById
with
  "album_user" as (
    select
      *
    from
      "album_user"
    where
      "album_user"."albumId" = $1
  )
select
  "album".*,
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "album_user"."role",
          "album_user"."isFavorite",
          (
            select
              to_json(obj)
            from
              (
                select
                  "id",
                  "name",
                  "email",
                  "avatarColor",
                  "profileImagePath",
                  "profileChangedAt"
                from
                  (
                    select
                      1
                  ) as "dummy"
              ) as obj
          ) as "user"
        from
          "album_user"
          inner join "user" on "user"."id" = "album_user"."userId"
        where
          "album_user"."albumId" = "album"."id"
        order by
          "album_user"."role",
          "album_user"."userId" = $2 desc,
          "user"."name" asc
      ) 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" = $3
  and "album"."deletedAt" is null

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

-- AlbumRepository.getByAssetIds
select
  "album"."id",
  "album_asset"."assetId"
from
  "album"
  inner join "album_asset" on "album_asset"."albumId" = "album"."id"
where
  exists (
    select
    from
      "album_user"
    where
      "album_user"."albumId" = "album"."id"
      and "album_user"."userId" = $1
  )
  and "album_asset"."assetId" in ($2)
  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".*,
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "album_user"."role",
          "album_user"."isFavorite",
          (
            select
              to_json(obj)
            from
              (
                select
                  "id",
                  "name",
                  "email",
                  "avatarColor",
                  "profileImagePath",
                  "profileChangedAt"
                from
                  (
                    select
                      1
                  ) as "dummy"
              ) as obj
          ) as "user"
        from
          "album_user"
          inner join "user" on "user"."id" = "album_user"."userId"
        where
          "album_user"."albumId" = "album"."id"
        order by
          "album_user"."role",
          "album_user"."userId" = $1 desc,
          "user"."name" asc
      ) 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"
  inner join "album_user" on "album_user"."albumId" = "album"."id"
  and "album_user"."userId" = $2
  and "album_user"."role" = 'owner'
where
  "album"."deletedAt" is null
order by
  "album"."createdAt" desc

-- AlbumRepository.getShared
select
  "album".*,
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "album_user"."role",
          "album_user"."isFavorite",
          (
            select
              to_json(obj)
            from
              (
                select
                  "id",
                  "name",
                  "email",
                  "avatarColor",
                  "profileImagePath",
                  "profileChangedAt"
                from
                  (
                    select
                      1
                  ) as "dummy"
              ) as obj
          ) as "user"
        from
          "album_user"
          inner join "user" on "user"."id" = "album_user"."userId"
        where
          "album_user"."albumId" = "album"."id"
        order by
          "album_user"."role",
          "album_user"."userId" = $1 desc,
          "user"."name" asc
      ) 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"
  inner join (
    select
      "album_user"."albumId" as "id"
    from
      "album_user"
    where
      "album_user"."userId" = $2
      and "album_user"."albumId" in (
        select
          "album_user"."albumId"
        from
          "album_user"
        where
          "album_user"."role" != 'owner'
      )
    union
    select
      "shared_link"."albumId" as "id"
    from
      "shared_link"
    where
      "shared_link"."userId" = $3
      and "shared_link"."albumId" is not null
  ) as "matching" on "matching"."id" = "album"."id"
  inner join "album_user" on "album_user"."albumId" = "album"."id"
  and "album_user"."role" = 'owner'
where
  "album"."deletedAt" is null
order by
  "album"."createdAt" desc

-- AlbumRepository.getFavorites
select
  "album".*,
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "album_user"."role",
          "album_user"."isFavorite",
          (
            select
              to_json(obj)
            from
              (
                select
                  "id",
                  "name",
                  "email",
                  "avatarColor",
                  "profileImagePath",
                  "profileChangedAt"
                from
                  (
                    select
                      1
                  ) as "dummy"
              ) as obj
          ) as "user"
        from
          "album_user"
          inner join "user" on "user"."id" = "album_user"."userId"
        where
          "album_user"."albumId" = "album"."id"
        order by
          "album_user"."role",
          "album_user"."userId" = $1 desc,
          "user"."name" asc
      ) 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"
  inner join "album_user" on "album_user"."albumId" = "album"."id"
  and "album_user"."userId" = $2
  and "album_user"."isFavorite" = true
where
  "album"."deletedAt" is null
order by
  "album"."createdAt" desc

-- AlbumRepository.getNotShared
select
  "album".*,
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "shared_link".*
        from
          "shared_link"
        where
          "shared_link"."albumId" = "album"."id"
      ) as agg
  ) as "sharedLinks",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "album_user"."role",
          "album_user"."isFavorite",
          (
            select
              to_json(obj)
            from
              (
                select
                  "id",
                  "name",
                  "email",
                  "avatarColor",
                  "profileImagePath",
                  "profileChangedAt"
                from
                  (
                    select
                      1
                  ) as "dummy"
              ) as obj
          ) as "user"
        from
          "album_user"
          inner join "user" on "user"."id" = "album_user"."userId"
        where
          "album_user"."albumId" = "album"."id"
        order by
          "album_user"."role",
          "album_user"."userId" = $1 desc,
          "user"."name" asc
      ) as agg
  ) as "albumUsers"
from
  "album"
  inner join "album_user" on "album_user"."albumId" = "album"."id"
  and "album_user"."userId" = $2
  and "album_user"."role" = 'owner'
where
  "album"."deletedAt" is null
  and not exists (
    select
    from
      "album_user" as "au"
    where
      "au"."albumId" = "album"."id"
      and "au"."role" != 'owner'
  )
  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.addAssetIds
insert into
  "album_asset"
select
  $1::uuid as "albumId",
  unnest($2::uuid[]) as "assetId"
from
  (
    select
      1
  ) as "dummy"
on conflict do nothing

-- AlbumRepository.create
with
  "album" as (
    insert into
      "album" ("albumName")
    values
      ($1)
    returning
      *
  ),
  "album_user" as (
    insert into
      "album_user"
    select
      "album"."id" as "albumId",
      unnest($2::uuid[]) as "userId",
      unnest($3::album_user_role_enum[]) as "role"
    from
      "album"
    returning
      "album_user"."albumId",
      "album_user"."userId",
      "album_user"."role",
      "album_user"."isFavorite"
  ),
  "album_asset" as (
    insert into
      "album_asset"
    select
      "album"."id" as "albumId",
      unnest($4::uuid[]) as "assetId"
    from
      "album"
    on conflict do nothing
    returning
      "album_asset"."albumId",
      "album_asset"."assetId"
  )
select
  "album".*,
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "album_user"."role",
          "album_user"."isFavorite",
          (
            select
              to_json(obj)
            from
              (
                select
                  "id",
                  "name",
                  "email",
                  "avatarColor",
                  "profileImagePath",
                  "profileChangedAt"
                from
                  (
                    select
                      1
                  ) as "dummy"
              ) as obj
          ) as "user"
        from
          "album_user"
          inner join "user" on "user"."id" = "album_user"."userId"
        where
          "album_user"."albumId" = "album"."id"
        order by
          "album_user"."role",
          "user"."name" asc
      ) as agg
  ) as "albumUsers",
  (
    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"

-- 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
