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

-- AssetJobRepository.getForSearchDuplicatesJob
select
  "id",
  "type",
  "ownerId",
  "duplicateId",
  "stackId",
  "visibility",
  "smart_search"."embedding"
from
  "asset"
  left join "smart_search" on "asset"."id" = "smart_search"."assetId"
where
  "asset"."id" = $1::uuid
limit
  $2

-- AssetJobRepository.getForSidecarWriteJob
select
  "id",
  "sidecarPath",
  "originalPath",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "tag"."value"
        from
          "tag"
          inner join "tag_asset" on "tag"."id" = "tag_asset"."tagsId"
        where
          "asset"."id" = "tag_asset"."assetsId"
      ) as agg
  ) as "tags"
from
  "asset"
where
  "asset"."id" = $1::uuid
limit
  $2

-- AssetJobRepository.streamForThumbnailJob
select
  "asset"."id",
  "asset"."thumbhash",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset_file"."id",
          "asset_file"."path",
          "asset_file"."type"
        from
          "asset_file"
        where
          "asset_file"."assetId" = "asset"."id"
      ) as agg
  ) as "files"
from
  "asset"
  inner join "asset_job_status" on "asset_job_status"."assetId" = "asset"."id"
where
  "asset"."deletedAt" is null
  and "asset"."visibility" != $1
  and (
    "asset_job_status"."previewAt" is null
    or "asset_job_status"."thumbnailAt" is null
    or "asset"."thumbhash" is null
  )

-- AssetJobRepository.getForMigrationJob
select
  "asset"."id",
  "asset"."ownerId",
  "asset"."encodedVideoPath",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset_file"."id",
          "asset_file"."path",
          "asset_file"."type"
        from
          "asset_file"
        where
          "asset_file"."assetId" = "asset"."id"
      ) as agg
  ) as "files"
from
  "asset"
where
  "asset"."id" = $1

-- AssetJobRepository.getForGenerateThumbnailJob
select
  "asset"."id",
  "asset"."visibility",
  "asset"."originalFileName",
  "asset"."originalPath",
  "asset"."ownerId",
  "asset"."thumbhash",
  "asset"."type",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset_file"."id",
          "asset_file"."path",
          "asset_file"."type"
        from
          "asset_file"
        where
          "asset_file"."assetId" = "asset"."id"
      ) as agg
  ) as "files",
  to_json("asset_exif") as "exifInfo"
from
  "asset"
  inner join "asset_exif" on "asset"."id" = "asset_exif"."assetId"
where
  "asset"."id" = $1

-- AssetJobRepository.getForMetadataExtraction
select
  "asset"."id",
  "asset"."checksum",
  "asset"."deviceAssetId",
  "asset"."deviceId",
  "asset"."fileCreatedAt",
  "asset"."fileModifiedAt",
  "asset"."isExternal",
  "asset"."visibility",
  "asset"."libraryId",
  "asset"."livePhotoVideoId",
  "asset"."localDateTime",
  "asset"."originalFileName",
  "asset"."originalPath",
  "asset"."ownerId",
  "asset"."sidecarPath",
  "asset"."type",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset_face".*
        from
          "asset_face"
        where
          "asset_face"."assetId" = "asset"."id"
          and "asset_face"."deletedAt" is null
      ) as agg
  ) as "faces"
from
  "asset"
where
  "asset"."id" = $1

-- AssetJobRepository.getAlbumThumbnailFiles
select
  "asset_file"."id",
  "asset_file"."path",
  "asset_file"."type"
from
  "asset_file"
where
  "asset_file"."assetId" = $1
  and "asset_file"."type" = $2

-- AssetJobRepository.streamForSearchDuplicates
select
  "asset"."id"
from
  "asset"
  inner join "smart_search" on "asset"."id" = "smart_search"."assetId"
  inner join "asset_job_status" as "job_status" on "job_status"."assetId" = "asset"."id"
where
  "asset"."deletedAt" is null
  and "asset"."visibility" in ('archive', 'timeline')
  and "job_status"."duplicatesDetectedAt" is null

-- AssetJobRepository.streamForEncodeClip
select
  "asset"."id"
from
  "asset"
  inner join "asset_job_status" as "job_status" on "assetId" = "asset"."id"
where
  "asset"."visibility" != $1
  and "asset"."deletedAt" is null
  and "job_status"."previewAt" is not null
  and not exists (
    select
    from
      "smart_search"
    where
      "assetId" = "asset"."id"
  )

-- AssetJobRepository.getForClipEncoding
select
  "asset"."id",
  "asset"."visibility",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset_file"."id",
          "asset_file"."path",
          "asset_file"."type"
        from
          "asset_file"
        where
          "asset_file"."assetId" = "asset"."id"
          and "asset_file"."type" = $1
      ) as agg
  ) as "files"
from
  "asset"
where
  "asset"."id" = $2

-- AssetJobRepository.getForDetectFacesJob
select
  "asset"."id",
  "asset"."visibility",
  to_json("asset_exif") as "exifInfo",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset_face".*
        from
          "asset_face"
        where
          "asset_face"."assetId" = "asset"."id"
      ) as agg
  ) as "faces",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset_file"."id",
          "asset_file"."path",
          "asset_file"."type"
        from
          "asset_file"
        where
          "asset_file"."assetId" = "asset"."id"
          and "asset_file"."type" = $1
      ) as agg
  ) as "files"
from
  "asset"
  inner join "asset_exif" on "asset"."id" = "asset_exif"."assetId"
where
  "asset"."id" = $2

-- AssetJobRepository.getForSyncAssets
select
  "asset"."id",
  "asset"."isOffline",
  "asset"."libraryId",
  "asset"."originalPath",
  "asset"."status",
  "asset"."fileModifiedAt"
from
  "asset"
where
  "asset"."id" = any ($1::uuid[])

-- AssetJobRepository.getForAssetDeletion
select
  "asset"."id",
  "asset"."visibility",
  "asset"."libraryId",
  "asset"."ownerId",
  "asset"."livePhotoVideoId",
  "asset"."sidecarPath",
  "asset"."encodedVideoPath",
  "asset"."originalPath",
  to_json("asset_exif") as "exifInfo",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset_face".*,
          "person" as "person"
        from
          "asset_face"
          left join lateral (
            select
              "person".*
            from
              "person"
            where
              "asset_face"."personId" = "person"."id"
          ) as "person" on true
        where
          "asset_face"."assetId" = "asset"."id"
          and "asset_face"."deletedAt" is null
      ) as agg
  ) as "faces",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset_file"."id",
          "asset_file"."path",
          "asset_file"."type"
        from
          "asset_file"
        where
          "asset_file"."assetId" = "asset"."id"
      ) as agg
  ) as "files",
  to_json("stacked_assets") as "stack"
from
  "asset"
  left join "asset_exif" on "asset"."id" = "asset_exif"."assetId"
  left join "stack" on "stack"."id" = "asset"."stackId"
  left join lateral (
    select
      "stack"."id",
      "stack"."primaryAssetId",
      array_agg("stacked") as "assets"
    from
      "asset" as "stacked"
    where
      "stacked"."deletedAt" is not null
      and "stacked"."visibility" = $1
      and "stacked"."stackId" = "stack"."id"
    group by
      "stack"."id"
  ) as "stacked_assets" on "stack"."id" is not null
where
  "asset"."id" = $2

-- AssetJobRepository.streamForVideoConversion
select
  "asset"."id"
from
  "asset"
where
  "asset"."type" = $1
  and (
    "asset"."encodedVideoPath" is null
    or "asset"."encodedVideoPath" = $2
  )
  and "asset"."visibility" != $3
  and "asset"."deletedAt" is null

-- AssetJobRepository.getForVideoConversion
select
  "asset"."id",
  "asset"."ownerId",
  "asset"."originalPath",
  "asset"."encodedVideoPath"
from
  "asset"
where
  "asset"."id" = $1
  and "asset"."type" = $2

-- AssetJobRepository.streamForMetadataExtraction
select
  "asset"."id"
from
  "asset"
  left join "asset_job_status" on "asset_job_status"."assetId" = "asset"."id"
where
  (
    "asset_job_status"."metadataExtractedAt" is null
    or "asset_job_status"."assetId" is null
  )
  and "asset"."deletedAt" is null

-- AssetJobRepository.getForStorageTemplateJob
select
  "asset"."id",
  "asset"."ownerId",
  "asset"."type",
  "asset"."checksum",
  "asset"."originalPath",
  "asset"."isExternal",
  "asset"."sidecarPath",
  "asset"."originalFileName",
  "asset"."livePhotoVideoId",
  "asset"."fileCreatedAt",
  "asset_exif"."timeZone",
  "asset_exif"."fileSizeInByte"
from
  "asset"
  inner join "asset_exif" on "asset"."id" = "asset_exif"."assetId"
where
  "asset"."deletedAt" is null
  and "asset"."id" = $1

-- AssetJobRepository.streamForStorageTemplateJob
select
  "asset"."id",
  "asset"."ownerId",
  "asset"."type",
  "asset"."checksum",
  "asset"."originalPath",
  "asset"."isExternal",
  "asset"."sidecarPath",
  "asset"."originalFileName",
  "asset"."livePhotoVideoId",
  "asset"."fileCreatedAt",
  "asset_exif"."timeZone",
  "asset_exif"."fileSizeInByte"
from
  "asset"
  inner join "asset_exif" on "asset"."id" = "asset_exif"."assetId"
where
  "asset"."deletedAt" is null

-- AssetJobRepository.streamForDeletedJob
select
  "id",
  "isOffline"
from
  "asset"
where
  "asset"."deletedAt" <= $1

-- AssetJobRepository.streamForSidecar
select
  "asset"."id"
from
  "asset"
where
  (
    "asset"."sidecarPath" = $1
    or "asset"."sidecarPath" is null
  )
  and "asset"."visibility" != $2

-- AssetJobRepository.streamForDetectFacesJob
select
  "asset"."id"
from
  "asset"
  inner join "asset_job_status" as "job_status" on "assetId" = "asset"."id"
where
  "asset"."visibility" != $1
  and "asset"."deletedAt" is null
  and "job_status"."previewAt" is not null
  and "job_status"."facesRecognizedAt" is null
order by
  "asset"."createdAt" desc

-- AssetJobRepository.streamForMigrationJob
select
  "id"
from
  "asset"
where
  "asset"."deletedAt" is null
