-- 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",
  (
    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"."usersId"
              ) as obj
          ) as "user"
        from
          "album_user"
        where
          "album_user"."albumsId" = "album"."id"
      ) as agg
  ) as "albumUsers",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          *
        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"."assetsId" = "asset"."id"
        where
          "album_asset"."albumsId" = "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" = $1
  and "album"."deletedAt" is null

-- AlbumRepository.getByAssetId
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",
  (
    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"."usersId"
              ) as obj
          ) as "user"
        from
          "album_user"
        where
          "album_user"."albumsId" = "album"."id"
      ) as agg
  ) as "albumUsers"
from
  "album"
  inner join "album_asset" on "album_asset"."albumsId" = "album"."id"
where
  (
    "album"."ownerId" = $1
    or exists (
      select
      from
        "album_user"
      where
        "album_user"."albumsId" = "album"."id"
        and "album_user"."usersId" = $2
    )
  )
  and "album_asset"."assetsId" = $3
  and "album"."deletedAt" is null
order by
  "album"."createdAt" desc,
  "album"."createdAt" desc

-- AlbumRepository.getMetadataForIds
select
  "album_asset"."albumsId" 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"."assetsId" = "asset"."id"
where
  "asset"."visibility" in ('archive', 'timeline')
  and "album_asset"."albumsId" in ($1)
  and "asset"."deletedAt" is null
group by
  "album_asset"."albumsId"

-- AlbumRepository.getOwned
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",
  (
    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"."usersId"
              ) as obj
          ) as "user"
        from
          "album_user"
        where
          "album_user"."albumsId" = "album"."id"
      ) as agg
  ) as "albumUsers",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          *
        from
          "shared_link"
        where
          "shared_link"."albumId" = "album"."id"
      ) as agg
  ) as "sharedLinks"
from
  "album"
where
  "album"."ownerId" = $1
  and "album"."deletedAt" is null
order by
  "album"."createdAt" desc

-- AlbumRepository.getShared
select
  "album".*,
  (
    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"."usersId"
              ) as obj
          ) as "user"
        from
          "album_user"
        where
          "album_user"."albumsId" = "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
          *
        from
          "shared_link"
        where
          "shared_link"."albumId" = "album"."id"
      ) as agg
  ) as "sharedLinks"
from
  "album"
where
  (
    exists (
      select
      from
        "album_user"
      where
        "album_user"."albumsId" = "album"."id"
        and (
          "album"."ownerId" = $1
          or "album_user"."usersId" = $2
        )
    )
    or exists (
      select
      from
        "shared_link"
      where
        "shared_link"."albumId" = "album"."id"
        and "shared_link"."userId" = $3
    )
  )
  and "album"."deletedAt" is null
order by
  "album"."createdAt" desc

-- AlbumRepository.getNotShared
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"
from
  "album"
where
  "album"."ownerId" = $1
  and "album"."deletedAt" is null
  and not exists (
    select
    from
      "album_user"
    where
      "album_user"."albumsId" = "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"."assetsId" in ($1)

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