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

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

-- PersonRepository.unassignFaces
update "asset_faces"
set
  "personId" = $1
where
  "asset_faces"."sourceType" = $2
VACUUM
ANALYZE asset_faces,
face_search,
person
REINDEX TABLE asset_faces
REINDEX TABLE person

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

-- PersonRepository.deleteFaces
delete from "asset_faces"
where
  "asset_faces"."sourceType" = $1
VACUUM
ANALYZE asset_faces,
face_search,
person
REINDEX TABLE asset_faces
REINDEX TABLE person

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

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

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

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

-- PersonRepository.getDataForThumbnailGenerationJob
select
  "person"."ownerId",
  "asset_faces"."boundingBoxX1" as "x1",
  "asset_faces"."boundingBoxY1" as "y1",
  "asset_faces"."boundingBoxX2" as "x2",
  "asset_faces"."boundingBoxY2" as "y2",
  "asset_faces"."imageWidth" as "oldWidth",
  "asset_faces"."imageHeight" as "oldHeight",
  "exif"."exifImageWidth",
  "exif"."exifImageHeight",
  "assets"."type",
  "assets"."originalPath",
  "asset_files"."path" as "previewPath"
from
  "person"
  inner join "asset_faces" on "asset_faces"."id" = "person"."faceAssetId"
  inner join "assets" on "asset_faces"."assetId" = "assets"."id"
  inner join "exif" on "exif"."assetId" = "assets"."id"
  inner join "asset_files" on "asset_files"."assetId" = "assets"."id"
where
  "person"."id" = $1
  and "asset_faces"."deletedAt" is null
  and "asset_files"."type" = $2
  and "exif"."exifImageWidth" > $3
  and "exif"."exifImageHeight" > $4

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

-- PersonRepository.getByName
select
  "person".*
from
  "person"
where
  (
    "person"."ownerId" = $1
    and (
      lower("person"."name") like $2
      or lower("person"."name") like $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 ("assets"."id")) as "count"
from
  "asset_faces"
  left join "assets" on "assets"."id" = "asset_faces"."assetId"
  and "asset_faces"."personId" = $1
  and "assets"."isArchived" = $2
  and "assets"."deletedAt" is null
where
  "asset_faces"."deletedAt" is null

-- PersonRepository.getNumberOfPeople
select
  count(distinct ("person"."id")) as "total",
  count(distinct ("person"."id")) filter (
    where
      "person"."isHidden" = $1
  ) as "hidden"
from
  "person"
  inner join "asset_faces" on "asset_faces"."personId" = "person"."id"
  inner join "assets" on "assets"."id" = "asset_faces"."assetId"
  and "assets"."deletedAt" is null
  and "assets"."isArchived" = $2
where
  "person"."ownerId" = $3
  and "asset_faces"."deletedAt" is null

-- 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_faces".*,
  (
    select
      to_json(obj)
    from
      (
        select
          "assets".*
        from
          "assets"
        where
          "assets"."id" = "asset_faces"."assetId"
      ) as obj
  ) as "asset",
  (
    select
      to_json(obj)
    from
      (
        select
          "person".*
        from
          "person"
        where
          "person"."id" = "asset_faces"."personId"
      ) as obj
  ) as "person"
from
  "asset_faces"
where
  "asset_faces"."assetId" in ($1)
  and "asset_faces"."personId" in ($2)
  and "asset_faces"."deletedAt" is null

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

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

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

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