package repository

const querySelectSongById = `
select
  id
  ,track_number
  ,title
  ,artist
  ,album
  ,duration
  ,modified_date
  ,base_path
  ,relative_path
from songs
where id = ANY($1)
`

const querySelectArtistsOrdered = `
select distinct artist
from songs
order by artist
limit $1
offset $2
`

const queryCountArtists = `
select count(*) as count from (
  select distinct artist from songs
) distinct_artists
`

const querySelectAlbumsByArtist = `
select distinct album
from songs
where artist = $1
order by album
`

const querySelectSongsByArtist = `
select
  id
  ,track_number
  ,title
  ,artist
  ,album
  ,duration
from songs
where artist = $1
order by album, track_number, title, id
`

const queryInsertSongs = `
insert into songs (
  track_number
  ,title
  ,artist
  ,album
  ,duration
  ,modified_date
  ,base_path
  ,relative_path
)
select * from unnest(
  $1::integer[]
  ,$2::varchar[]
  ,$3::varchar[]
  ,$4::varchar[]
  ,$5::integer[]
  ,$6::bigint[]
  ,$7::varchar[]
  ,$8::varchar[]
)
on conflict (base_path, relative_path) do update
set
  track_number = excluded.track_number
  ,title = excluded.title
  ,artist = excluded.artist
  ,album = excluded.album
  ,duration = excluded.duration
  ,modified_date = excluded.modified_date
`

const querySelectNextSong = `
select
  s1.id
  ,s1.track_number
  ,s1.title
  ,s1.artist
  ,s1.album
  ,s1.duration

from (
  select * from songs where id = $1
) s0

left join songs s1 on (
  s1.artist > s0.artist
  or (s1.artist = s0.artist
    and s1.album > s0.album
  )
  or (s1.artist = s0.artist
    and s1.album = s0.album
    and s1.track_number > s0.track_number
  )
  or (s1.artist = s0.artist
    and s1.album = s0.album
    and s1.track_number = s0.track_number
    and s1.title > s0.title
  )
  or (s1.artist = s0.artist
    and s1.album = s0.album
    and s1.track_number = s0.track_number
    and s1.title = s0.title
    and s1.id > s0.id
  )
)

order by
  s1.artist
  ,s1.album
  ,s1.track_number
  ,s1.title
  ,s1.id

limit 1
`

const querySelectPrevSong = `
select
  s1.id
  ,s1.track_number
  ,s1.title
  ,s1.artist
  ,s1.album
  ,s1.duration

from (
  select * from songs where id = $1
) s0

left join songs s1 on (
  s1.artist < s0.artist
  or (s1.artist = s0.artist
    and s1.album < s0.album
  )
  or (s1.artist = s0.artist
    and s1.album = s0.album
    and s1.track_number < s0.track_number
  )
  or (s1.artist = s0.artist
    and s1.album = s0.album
    and s1.track_number = s0.track_number
    and s1.title < s0.title
  )
  or (s1.artist = s0.artist
    and s1.album = s0.album
    and s1.track_number = s0.track_number
    and s1.title = s0.title
    and s1.id < s0.id
  )
)

order by
  s1.artist desc
  ,s1.album desc
  ,s1.track_number desc
  ,s1.title desc
  ,s1.id desc

limit 1
`
