-- 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",
  "originalPath",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset_file"."id",
          "asset_file"."path",
          "asset_file"."type",
          "asset_file"."isEdited"
        from
          "asset_file"
        where
          "asset_file"."assetId" = "asset"."id"
          and "asset_file"."type" = $1
      ) 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" = $2::uuid
limit
  $3

-- AssetJobRepository.getForSidecarCheckJob
select
  "id",
  "originalPath",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset_file"."id",
          "asset_file"."path",
          "asset_file"."type",
          "asset_file"."isEdited"
        from
          "asset_file"
        where
          "asset_file"."assetId" = "asset"."id"
          and "asset_file"."type" = $1
      ) as agg
  ) as "files"
from
  "asset"
where
  "asset"."id" = $2::uuid
limit
  $3

-- AssetJobRepository.streamForThumbnailJob
select
  "asset"."id",
  "asset"."isEdited"
from
  "asset"
  inner join "asset_job_status" on "asset_job_status"."assetId" = "asset"."id"
where
  "asset"."deletedAt" is null
  and "asset"."visibility" != 'hidden'
  and (
    not exists (
      select
      from
        "asset_file"
      where
        "assetId" = "asset"."id"
        and "type" = 'thumbnail'
    )
    or not exists (
      select
      from
        "asset_file"
      where
        "assetId" = "asset"."id"
        and "type" = 'preview'
    )
    or (
      "asset"."isEdited" = true
      and not exists (
        select
        from
          "asset_file"
        where
          "assetId" = "asset"."id"
          and "type" = 'fullsize'
          and "asset_file"."isEdited" = true
      )
    )
    or "asset"."thumbhash" is null
    or (
      not exists (
        select
        from
          "asset_file"
        where
          "assetId" = "asset"."id"
          and "type" = 'fullsize'
      )
      and f_unaccent (asset."originalFileName") like any (
        array[
          '%.3fr',
          '%.ari',
          '%.arw',
          '%.cap',
          '%.cin',
          '%.cr2',
          '%.cr3',
          '%.crw',
          '%.dcr',
          '%.dng',
          '%.erf',
          '%.fff',
          '%.iiq',
          '%.k25',
          '%.kdc',
          '%.mrw',
          '%.nef',
          '%.nrw',
          '%.orf',
          '%.ori',
          '%.pef',
          '%.psd',
          '%.raf',
          '%.raw',
          '%.rw2',
          '%.rwl',
          '%.sr2',
          '%.srf',
          '%.srw',
          '%.x3f',
          '%.heic',
          '%.heif',
          '%.hif',
          '%.insp',
          '%.jp2',
          '%.jpe',
          '%.jxl',
          '%.svg',
          '%.tif',
          '%.tiff'
        ]::text[]
      )
    )
  )

-- 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",
          "asset_file"."isEdited"
        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",
          "asset_file"."isEdited",
          "asset_file"."isProgressive",
          "asset_file"."isTransparent"
        from
          "asset_file"
        where
          "asset_file"."assetId" = "asset"."id"
          and "asset_file"."type" in ($1, $2, $3)
      ) as agg
  ) as "files",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset_edit"."action",
          "asset_edit"."parameters"
        from
          "asset_edit"
        where
          "asset_edit"."assetId" = "asset"."id"
      ) as agg
  ) as "edits",
  to_json("asset_exif") as "exifInfo"
from
  "asset"
  inner join "asset_exif" on "asset"."id" = "asset_exif"."assetId"
where
  "asset"."id" = $4

-- 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"."type",
  "asset"."width",
  "asset"."height",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset_face".*
        from
          "asset_face"
        where
          "asset_face"."assetId" = "asset"."id"
          and "asset_face"."deletedAt" is null
          and "asset_face"."isVisible" = $1
      ) as agg
  ) as "faces",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset_file"."id",
          "asset_file"."path",
          "asset_file"."type",
          "asset_file"."isEdited"
        from
          "asset_file"
        where
          "asset_file"."assetId" = "asset"."id"
          and "asset_file"."type" = $2
      ) as agg
  ) as "files"
from
  "asset"
where
  "asset"."id" = $3

-- AssetJobRepository.getLockedPropertiesForMetadataExtraction
select
  "asset_exif"."lockedProperties"
from
  "asset_exif"
where
  "asset_exif"."assetId" = $1

-- AssetJobRepository.getAlbumThumbnailFiles
select
  "asset_file"."id",
  "asset_file"."path",
  "asset_file"."type",
  "asset_file"."isEdited"
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 exists (
    select
    from
      "asset_file"
    where
      "assetId" = "asset"."id"
      and "asset_file"."type" = $2
  )
  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",
          "asset_file"."isEdited"
        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",
          "asset_file"."isEdited"
        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.getForOcr
select
  "asset"."visibility",
  (
    select
      "asset_file"."path"
    from
      "asset_file"
    where
      "asset_file"."assetId" = "asset"."id"
      and "asset_file"."type" = $1
  ) as "previewFile"
from
  "asset"
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"."encodedVideoPath",
  "asset"."originalPath",
  "asset"."isOffline",
  to_json("asset_exif") as "exifInfo",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset_file"."id",
          "asset_file"."path",
          "asset_file"."type",
          "asset_file"."isEdited"
        from
          "asset_file"
        where
          "asset_file"."assetId" = "asset"."id"
      ) as agg
  ) as "files",
  to_json("stack_result") as "stack"
from
  "asset"
  left join "asset_exif" on "asset"."id" = "asset_exif"."assetId"
  left join lateral (
    select
      "stack"."id",
      "stack"."primaryAssetId",
      (
        select
          coalesce(json_agg(agg), '[]')
        from
          (
            select
              "stack_asset"."id"
            from
              "asset" as "stack_asset"
            where
              "stack_asset"."stackId" = "stack"."id"
              and "stack_asset"."id" != "stack"."primaryAssetId"
              and "stack_asset"."visibility" = $1
              and "stack_asset"."status" != $2
          ) as agg
      ) as "assets"
    from
      "stack"
    where
      "stack"."id" = "asset"."stackId"
  ) as "stack_result" on true
where
  "asset"."id" = $3

-- 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"."visibility",
  "asset"."originalFileName",
  "asset"."livePhotoVideoId",
  "asset"."fileCreatedAt",
  "asset_exif"."timeZone",
  "asset_exif"."fileSizeInByte",
  "asset_exif"."make",
  "asset_exif"."model",
  "asset_exif"."lensModel",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset_file"."id",
          "asset_file"."path",
          "asset_file"."type",
          "asset_file"."isEdited"
        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"."deletedAt" is null
  and "asset"."id" = $2
  and "asset"."visibility" != $3

-- AssetJobRepository.streamForStorageTemplateJob
select
  "asset"."id",
  "asset"."ownerId",
  "asset"."type",
  "asset"."checksum",
  "asset"."originalPath",
  "asset"."isExternal",
  "asset"."visibility",
  "asset"."originalFileName",
  "asset"."livePhotoVideoId",
  "asset"."fileCreatedAt",
  "asset_exif"."timeZone",
  "asset_exif"."fileSizeInByte",
  "asset_exif"."make",
  "asset_exif"."model",
  "asset_exif"."lensModel",
  (
    select
      coalesce(json_agg(agg), '[]')
    from
      (
        select
          "asset_file"."id",
          "asset_file"."path",
          "asset_file"."type",
          "asset_file"."isEdited"
        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"."deletedAt" is null
  and "asset"."visibility" != $2

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

-- AssetJobRepository.streamForSidecar
select
  "asset"."id"
from
  "asset"
where
  not exists (
    select
      "asset_file"."id"
    from
      "asset_file"
    where
      "asset_file"."assetId" = "asset"."id"
      and "asset_file"."type" = $1
  )

-- 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 exists (
    select
    from
      "asset_file"
    where
      "assetId" = "asset"."id"
      and "asset_file"."type" = $2
  )
order by
  "asset"."fileCreatedAt" desc

-- AssetJobRepository.streamForOcrJob
select
  "asset"."id"
from
  "asset"
  inner join "asset_job_status" on "asset_job_status"."assetId" = "asset"."id"
where
  "asset_job_status"."ocrAt" is null
  and "asset"."deletedAt" is null
  and "asset"."visibility" != $1

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