-- Add 1 refill and 1 spulna for each color as PLA Basic filaments
-- Run this with: psql $DATABASE_URL -f scripts/add-basic-refills.sql

-- First show what colors we have
SELECT name, hex FROM colors ORDER BY name;

-- Insert PLA Basic filaments with 1 refill and 1 spulna for each color that doesn't already have one
INSERT INTO filaments (tip, finish, boja, boja_hex, refill, spulna, kolicina, cena)
SELECT 
    'PLA' as tip,
    'Basic' as finish,
    c.name as boja,
    c.hex as boja_hex,
    1 as refill,
    1 as spulna,
    2 as kolicina,  -- 1 refill + 1 spulna
    '3999' as cena
FROM colors c
WHERE NOT EXISTS (
    SELECT 1 FROM filaments f 
    WHERE f.tip = 'PLA' 
    AND f.finish = 'Basic' 
    AND f.boja = c.name
)
ON CONFLICT DO NOTHING;

-- Update any existing PLA Basic filaments to have 1 refill and 1 spulna
UPDATE filaments
SET refill = 1,
    spulna = 1,
    kolicina = 2  -- Update quantity to reflect 1 refill + 1 spulna
WHERE tip = 'PLA' 
AND finish = 'Basic'
AND (refill = 0 OR spulna = 0);

-- Show summary
SELECT 
    'Total PLA Basic filaments with refills and spulna' as description,
    COUNT(*) as count 
FROM filaments 
WHERE tip = 'PLA' 
AND finish = 'Basic' 
AND refill = 1
AND spulna = 1;

-- Show all PLA Basic filaments
SELECT 
    boja as color,
    refill,
    spulna,
    kolicina as quantity,
    cena as price
FROM filaments 
WHERE tip = 'PLA' 
AND finish = 'Basic'
ORDER BY boja;