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

-- SearchRepository.searchMetadata
select
  "asset".*
from
  "asset"
  inner join "asset_exif" on "asset"."id" = "asset_exif"."assetId"
where
  "asset"."visibility" = $1
  and "asset"."fileCreatedAt" >= $2
  and "asset_exif"."lensModel" = $3
  and "asset"."ownerId" = any ($4::uuid[])
  and "asset"."isFavorite" = $5
  and "asset"."deletedAt" is null
order by
  "asset"."fileCreatedAt" desc
limit
  $6
offset
  $7

-- SearchRepository.searchStatistics
select
  count(*) as "total"
from
  "asset"
  inner join "asset_exif" on "asset"."id" = "asset_exif"."assetId"
where
  "asset"."visibility" = $1
  and "asset"."fileCreatedAt" >= $2
  and "asset_exif"."lensModel" = $3
  and "asset"."ownerId" = any ($4::uuid[])
  and "asset"."isFavorite" = $5
  and "asset"."deletedAt" is null

-- SearchRepository.searchRandom
select
  "asset".*
from
  "asset"
  inner join "asset_exif" on "asset"."id" = "asset_exif"."assetId"
where
  "asset"."visibility" = $1
  and "asset"."fileCreatedAt" >= $2
  and "asset_exif"."lensModel" = $3
  and "asset"."ownerId" = any ($4::uuid[])
  and "asset"."isFavorite" = $5
  and "asset"."deletedAt" is null
order by
  random()
limit
  $6

-- SearchRepository.searchLargeAssets
select
  "asset".*,
  to_json("asset_exif") as "exifInfo"
from
  "asset"
  inner join "asset_exif" on "asset"."id" = "asset_exif"."assetId"
where
  "asset"."visibility" = $1
  and "asset"."fileCreatedAt" >= $2
  and "asset_exif"."lensModel" = $3
  and "asset"."ownerId" = any ($4::uuid[])
  and "asset"."isFavorite" = $5
  and "asset"."deletedAt" is null
  and "asset_exif"."fileSizeInByte" > $6
order by
  "asset_exif"."fileSizeInByte" desc
limit
  $7

-- SearchRepository.searchSmart
begin
set
  local vchordrq.probes = 1
select
  "asset".*
from
  "asset"
  inner join "asset_exif" on "asset"."id" = "asset_exif"."assetId"
  inner join "smart_search" on "asset"."id" = "smart_search"."assetId"
where
  "asset"."visibility" = $1
  and "asset"."fileCreatedAt" >= $2
  and "asset_exif"."lensModel" = $3
  and "asset"."ownerId" = any ($4::uuid[])
  and "asset"."isFavorite" = $5
  and "asset"."deletedAt" is null
order by
  smart_search.embedding <=> $6
limit
  $7
offset
  $8
commit

-- SearchRepository.getEmbedding
select
  *
from
  "smart_search"
where
  "assetId" = $1

-- SearchRepository.searchFaces
begin
set
  local vchordrq.probes = 1
with
  "cte" as (
    select
      "asset_face"."id",
      "asset_face"."personId",
      face_search.embedding <=> $1 as "distance"
    from
      "asset_face"
      inner join "asset" on "asset"."id" = "asset_face"."assetId"
      inner join "face_search" on "face_search"."faceId" = "asset_face"."id"
      left join "person" on "person"."id" = "asset_face"."personId"
    where
      "asset"."ownerId" = any ($2::uuid[])
      and "asset"."deletedAt" is null
    order by
      "distance"
    limit
      $3
  )
select
  *
from
  "cte"
where
  "cte"."distance" <= $4
commit

-- SearchRepository.searchPlaces
select
  *
from
  "geodata_places"
where
  f_unaccent (name) %>> f_unaccent ($1)
  or f_unaccent ("admin2Name") %>> f_unaccent ($2)
  or f_unaccent ("admin1Name") %>> f_unaccent ($3)
  or f_unaccent ("alternateNames") %>> f_unaccent ($4)
order by
  coalesce(f_unaccent (name) <->>> f_unaccent ($5), 0.1) + coalesce(
    f_unaccent ("admin2Name") <->>> f_unaccent ($6),
    0.1
  ) + coalesce(
    f_unaccent ("admin1Name") <->>> f_unaccent ($7),
    0.1
  ) + coalesce(
    f_unaccent ("alternateNames") <->>> f_unaccent ($8),
    0.1
  )
limit
  $9

-- SearchRepository.getAssetsByCity
with recursive
  "cte" as (
    (
      select
        "city",
        "assetId"
      from
        "asset_exif"
        inner join "asset" on "asset"."id" = "asset_exif"."assetId"
      where
        "asset"."ownerId" = any ($1::uuid[])
        and "asset"."visibility" = $2
        and "asset"."type" = $3
        and "asset"."deletedAt" is null
      order by
        "city"
      limit
        $4
    )
    union all
    (
      select
        "l"."city",
        "l"."assetId"
      from
        "cte"
        inner join lateral (
          select
            "city",
            "assetId"
          from
            "asset_exif"
            inner join "asset" on "asset"."id" = "asset_exif"."assetId"
          where
            "asset"."ownerId" = any ($5::uuid[])
            and "asset"."visibility" = $6
            and "asset"."type" = $7
            and "asset"."deletedAt" is null
            and "asset_exif"."city" > "cte"."city"
          order by
            "city"
          limit
            $8
        ) as "l" on true
    )
  )
select
  "asset".*,
  to_jsonb("asset_exif") as "exifInfo"
from
  "asset"
  inner join "asset_exif" on "asset"."id" = "asset_exif"."assetId"
  inner join "cte" on "asset"."id" = "cte"."assetId"
order by
  "asset_exif"."city"

-- SearchRepository.getStates
select distinct
  on ("state") "state"
from
  "asset_exif"
  inner join "asset" on "asset"."id" = "asset_exif"."assetId"
where
  "ownerId" = any ($1::uuid[])
  and "visibility" = $2
  and "deletedAt" is null
  and "state" is not null
  and "state" != $3

-- SearchRepository.getCities
select distinct
  on ("city") "city"
from
  "asset_exif"
  inner join "asset" on "asset"."id" = "asset_exif"."assetId"
where
  "ownerId" = any ($1::uuid[])
  and "visibility" = $2
  and "deletedAt" is null
  and "city" is not null
  and "city" != $3

-- SearchRepository.getCameraMakes
select distinct
  on ("make") "make"
from
  "asset_exif"
  inner join "asset" on "asset"."id" = "asset_exif"."assetId"
where
  "ownerId" = any ($1::uuid[])
  and "visibility" = $2
  and "deletedAt" is null
  and "make" is not null
  and "make" != $3

-- SearchRepository.getCameraModels
select distinct
  on ("model") "model"
from
  "asset_exif"
  inner join "asset" on "asset"."id" = "asset_exif"."assetId"
where
  "ownerId" = any ($1::uuid[])
  and "visibility" = $2
  and "deletedAt" is null
  and "model" is not null
  and "model" != $3

-- SearchRepository.getCameraLensModels
select distinct
  on ("lensModel") "lensModel"
from
  "asset_exif"
  inner join "asset" on "asset"."id" = "asset_exif"."assetId"
where
  "ownerId" = any ($1::uuid[])
  and "visibility" = $2
  and "deletedAt" is null
  and "lensModel" is not null
  and "lensModel" != $3
