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

-- PersonRepository.reassignFaces
update "asset_face"
set
  "personId" = $1
where
  "asset_face"."personId" = $2

-- PersonRepository.delete
delete from "person"
where
  "person"."id" in ($1)

-- PersonRepository.getFileSamples
select
  "id",
  "thumbnailPath"
from
  "person"
where
  "thumbnailPath" != ''
limit
  3

-- PersonRepository.getAllForUser
select
  "person".*
from
  "person"
  inner join "asset_face" on "asset_face"."personId" = "person"."id"
  inner join "asset" on "asset_face"."assetId" = "asset"."id"
  and "asset"."visibility" = 'timeline'
  and "asset"."deletedAt" is null
where
  "person"."ownerId" = $1
  and "asset_face"."deletedAt" is null
  and "asset_face"."isVisible" is true
  and "person"."isHidden" = $2
group by
  "person"."id"
having
  (
    "person"."name" != $3
    or count("asset_face"."assetId") >= $4
  )
order by
  "person"."isHidden" asc,
  "person"."isFavorite" desc,
  NULLIF(person.name, '') is null asc,
  count("asset_face"."assetId") desc,
  NULLIF(person.name, '') asc nulls last,
  "person"."createdAt"
limit
  $5
offset
  $6

-- PersonRepository.getAllWithoutFaces
select
  "person".*
from
  "person"
  left join "asset_face" on "asset_face"."personId" = "person"."id"
where
  "asset_face"."deletedAt" is null
  and "asset_face"."isVisible" is true
group by
  "person"."id"
having
  count("asset_face"."assetId") = $1

-- PersonRepository.getFaces
select
  "asset_face".*,
  (
    select
      to_json(obj)
    from
      (
        select
          "person".*
        from
          "person"
        where
          "person"."id" = "asset_face"."personId"
      ) as obj
  ) as "person"
from
  "asset_face"
where
  "asset_face"."assetId" = $1
  and "asset_face"."deletedAt" is null
  and "asset_face"."isVisible" = $2
order by
  "asset_face"."boundingBoxX1" asc

-- PersonRepository.getFaceById
select
  "asset_face".*,
  (
    select
      to_json(obj)
    from
      (
        select
          "person".*
        from
          "person"
        where
          "person"."id" = "asset_face"."personId"
      ) as obj
  ) as "person"
from
  "asset_face"
where
  "asset_face"."id" = $1
  and "asset_face"."deletedAt" is null

-- PersonRepository.getFaceForFacialRecognitionJob
select
  "asset_face"."id",
  "asset_face"."personId",
  "asset_face"."sourceType",
  (
    select
      to_json(obj)
    from
      (
        select
          "asset"."ownerId",
          "asset"."visibility",
          "asset"."fileCreatedAt"
        from
          "asset"
        where
          "asset"."id" = "asset_face"."assetId"
      ) as obj
  ) as "asset",
  (
    select
      to_json(obj)
    from
      (
        select
          "face_search".*
        from
          "face_search"
        where
          "face_search"."faceId" = "asset_face"."id"
      ) as obj
  ) as "faceSearch"
from
  "asset_face"
where
  "asset_face"."id" = $1
  and "asset_face"."deletedAt" is null

-- PersonRepository.getDataForThumbnailGenerationJob
select
  "person"."ownerId",
  "asset_face"."boundingBoxX1" as "x1",
  "asset_face"."boundingBoxY1" as "y1",
  "asset_face"."boundingBoxX2" as "x2",
  "asset_face"."boundingBoxY2" as "y2",
  "asset_face"."imageWidth" as "oldWidth",
  "asset_face"."imageHeight" as "oldHeight",
  "asset"."type",
  "asset"."originalPath",
  "asset_exif"."orientation" as "exifOrientation",
  (
    select
      "asset_file"."path"
    from
      "asset_file"
    where
      "asset_file"."assetId" = "asset"."id"
      and "asset_file"."type" = 'preview'
      and "asset_file"."isEdited" = false
  ) as "previewPath"
from
  "person"
  inner join "asset_face" on "asset_face"."id" = "person"."faceAssetId"
  inner join "asset" on "asset_face"."assetId" = "asset"."id"
  left join "asset_exif" on "asset_exif"."assetId" = "asset"."id"
where
  "person"."id" = $1
  and "asset_face"."deletedAt" is null

-- PersonRepository.reassignFace
update "asset_face"
set
  "personId" = $1
where
  "asset_face"."id" = $2

-- PersonRepository.getByName
with
  "similarity_threshold" as (
    select
      set_config('pg_trgm.word_similarity_threshold', '0.5', true) as "thresh"
  )
select
  "person".*
from
  "similarity_threshold",
  "person"
where
  "person"."ownerId" = $1
  and f_unaccent ("person"."name") %> f_unaccent ($2)
order by
  f_unaccent ("person"."name") <->>> f_unaccent ($3)
limit
  $4

-- PersonRepository.getDistinctNames
select distinct
  on (lower("person"."name")) "person"."id",
  "person"."name"
from
  "person"
where
  (
    "person"."ownerId" = $1
    and "person"."name" != $2
  )

-- PersonRepository.getStatistics
select
  count(distinct ("asset"."id")) as "count"
from
  "asset_face"
  left join "asset" on "asset"."id" = "asset_face"."assetId"
  and "asset"."visibility" = 'timeline'
  and "asset"."deletedAt" is null
where
  "asset_face"."deletedAt" is null
  and "asset_face"."isVisible" is true
  and "asset_face"."personId" = $1

-- PersonRepository.getNumberOfPeople
select
  coalesce(count(*), 0) as "total",
  coalesce(
    count(*) filter (
      where
        "isHidden" = $1
    ),
    0
  ) as "hidden"
from
  "person"
where
  exists (
    select
    from
      "asset_face"
    where
      "asset_face"."personId" = "person"."id"
      and "asset_face"."deletedAt" is null
      and "asset_face"."isVisible" = $2
      and exists (
        select
        from
          "asset"
        where
          "asset"."id" = "asset_face"."assetId"
          and "asset"."visibility" = 'timeline'
          and "asset"."deletedAt" is null
      )
  )
  and "person"."ownerId" = $3

-- PersonRepository.refreshFaces
with
  "added_embeddings" as (
    insert into
      "face_search" ("faceId", "embedding")
    values
      ($1, $2)
  )
select
from
  (
    select
      1
  ) as "dummy"

-- PersonRepository.getFacesByIds
select
  "asset_face".*,
  (
    select
      to_json(obj)
    from
      (
        select
          "person".*
        from
          "person"
        where
          "person"."id" = "asset_face"."personId"
      ) as obj
  ) as "person"
from
  "asset_face"
where
  "asset_face"."assetId" in ($1)
  and "asset_face"."personId" in ($2)
  and "asset_face"."deletedAt" is null

-- PersonRepository.getRandomFace
select
  "asset_face".*
from
  "asset_face"
where
  "asset_face"."personId" = $1
  and "asset_face"."deletedAt" is null
  and "asset_face"."isVisible" is true

-- PersonRepository.getLatestFaceDate
select
  max("asset_job_status"."facesRecognizedAt")::text as "latestDate"
from
  "asset_job_status"

-- PersonRepository.deleteAssetFace
delete from "asset_face"
where
  "asset_face"."id" = $1

-- PersonRepository.softDeleteAssetFaces
update "asset_face"
set
  "deletedAt" = $1
where
  "asset_face"."id" = $2

-- PersonRepository.getForPeopleDelete
select
  "id",
  "thumbnailPath"
from
  "person"
where
  "id" in ($1)

-- PersonRepository.getForFeatureFaceUpdate
select
  "asset_face"."id"
from
  "asset_face"
  inner join "asset" on "asset"."id" = "asset_face"."assetId"
  and "asset"."isOffline" = $1
where
  "asset_face"."assetId" = $2
  and "asset_face"."personId" = $3
