-- name: GetCartridgeById :one
select c.id as id, c.name, c.meta
from cartridges c
where id = $1;

-- name: GetCartridges :many
select c.id as id, c.name, c.meta
from cartridges c
order by c.name;

-- name: CreateCartridge :one
insert into cartridges (name, meta)
values ($1, $2)
returning id;

-- name: DeleteCartridge :exec
delete
from cartridges
where id = $1;

-- name: CreateLoad :one
insert into loads (cartridge_id, col, powder_id, powder_gr, primer_id, bullet_id, photo, meta)
values ($1, $2, $3, $4, $5, $6, $7, $8)
returning id;

-- name: GetLoadById :one
select l.id    as id,
       c.id    as cartridge_id,
       c.name  as cartridge_name,
       c.meta  as cartridge_meta,
       l.col,
       p.id    as powder_id,
       p.name  as powder_name,
       p.meta  as powder_meta,
       l.powder_gr,
       pr.id   as primer_id,
       pr.name as primer_name,
       pr.meta as primer_meta
from public.loads l
         join cartridges c on l.cartridge_id = c.id
         join bullets b on l.bullet_id = b.id
         join powders p on l.powder_id = p.id
         join primers pr on l.primer_id = pr.id
where l.id = $1;

-- name: TotalLoads :one
select count(*)
from loads;

-- name: GetLoads :many
select l.id       as id,
       l.col,
       l.powder_gr,
       c.id       as cartridge_id,
       c.name     as cartridge_name,
       c.meta     as cartridge_meta,
       p.id       as powder_id,
       p.name     as powder_name,
       p.meta     as powder_meta,
       p_m.name   as powder_manufacturer_name,
       p_m.url    as powder_manufacturer_url,
       pr.id      as primer_id,
       pr.name    as primer_name,
       pr.meta    as primer_meta,
       pr_m.name  as primer_manufacturer_name,
       pr_m.url   as primer_manufacturer_url,
       b.id       as bullet_id,
       b.name     as bullet_name,
       b.meta     as bullet_meta,
       b.weight   as bullet_weight,
       b.diameter as bullet_diameter,
       b_m.name   as bullet_manufacturer_name,
       b_m.url    as bullet_manufacturer_url
from public.loads l
         join cartridges c on l.cartridge_id = c.id
         join bullets b on l.bullet_id = b.id
         join manufacturers b_m on b.manufacturer_id = b_m.id
         join powders p on l.powder_id = p.id
         join manufacturers p_m on p.manufacturer_id = p_m.id
         join primers pr on l.primer_id = pr.id
         join manufacturers pr_m on pr.manufacturer_id = pr_m.id
where 1 = 1

  -- Loose Search By Name
  AND (CASE WHEN @search_cartridge::bool THEN lower(c.name) like lower(@cartridge) ELSE TRUE END)
  AND (CASE WHEN @search_bullet::bool THEN lower(b.name) like lower(@bullet) ELSE TRUE END)
  AND (CASE WHEN @search_bullet_manufacturer::bool THEN lower(b_m.name) like lower(@bullet_manufacturer) ELSE TRUE END)
  AND (CASE WHEN @search_primer::bool THEN lower(pr.name) like lower(@primer) ELSE TRUE END)
  AND (CASE WHEN @search_primer_manufacturer::bool THEN lower(pr_m.name) like lower(@prime_manufacturer) ELSE TRUE END)
  AND (CASE WHEN @search_powder::bool THEN lower(p.name) like lower(@powder) ELSE TRUE END)
  AND (CASE WHEN @search_powder_manufacturer::bool THEN lower(p_m.name) like lower(@powder_manufacturer) ELSE TRUE END)

  -- Exact Search By ID
  AND (CASE WHEN @search_cartridge_id::bool THEN c.id = @cartridge_id::uuid ELSE TRUE END)
  AND (CASE WHEN @search_bullet_id::bool THEN b.id = @bullet_id::uuid ELSE TRUE END)
  AND (CASE WHEN @search_primer_id::bool THEN pr.id = @primer_id::uuid ELSE TRUE END)
  AND (CASE WHEN @search_powder_id::bool THEN p.id = @powder_id::uuid ELSE TRUE END)

order by (case
             when @order_by = 'cartridge_name' then c.name
             when @order_by = 'bullet_name' then b.name
             when @order_by = 'bullet_manufacturer_name' then b_m.name
             when @order_by = 'primer_manufacturer_name' then pr_m.name
             when @order_by = 'powder_manufacturer_name' then p_m.name
             when @order_by::text = 'powder_gr' then l.powder_gr
             end)

limit $1 offset $2;