-- EG.D tarif pro home-01 (FVE speciální režim)
INSERT INTO ems.distribution_tariff
    (distributor, code, name, has_dual_rate, vat_rate, notes)
VALUES
    ('EGD', 'custom_fve_home01',
     'EG.D FVE vlastní spotřeba – dvojsazba',
     true, 0.21,
     'Speciální tarif pro FVE instalaci home-01. VT okna dle smlouvy.');

-- Sazby – placeholder hodnoty, doplnit ze smlouvy/faktury
INSERT INTO ems.distribution_tariff_rate
    (tariff_id, rate_type, price_czk_kwh, valid_from)
SELECT
    id, 'NT', 0.2243, '2025-01-01'
FROM ems.distribution_tariff WHERE distributor='EGD' AND code='custom_fve_home01';

INSERT INTO ems.distribution_tariff_rate
    (tariff_id, rate_type, price_czk_kwh, valid_from)
SELECT
    id, 'VT', 0.74987, '2025-01-01'
FROM ems.distribution_tariff WHERE distributor='EGD' AND code='custom_fve_home01';

-- HDO kód pro home-01
INSERT INTO ems.hdo_code
    (distributor, code, description, valid_from, notes)
VALUES
    ('EGD', 'custom_fve_home01',
     'FVE home-01 – VT okna 09-10, 12-13, 16-17, 20-21 (každý den)',
     '2025-01-01',
     'Platí stejně workday i weekend. Při změně přidat nový záznam s novým valid_from.');

-- VT okna (4 okna, každý den)
INSERT INTO ems.hdo_code_window
    (hdo_code_id, day_type, rate_type, window_from, window_to)
SELECT
    hc.id, 'all', 'VT', w.wf::TIME, w.wt::TIME
FROM ems.hdo_code hc,
(VALUES
    ('09:00', '10:00'),
    ('12:00', '13:00'),
    ('16:00', '17:00'),
    ('20:00', '21:00')
) AS w(wf, wt)
WHERE hc.distributor = 'EGD' AND hc.code = 'custom_fve_home01';

-- Napojit home-01 na tarif a HDO kód
UPDATE ems.site_market_config SET
    tariff_id    = (SELECT id FROM ems.distribution_tariff
                    WHERE distributor='EGD' AND code='custom_fve_home01'),
    hdo_code_id  = (SELECT id FROM ems.hdo_code
                    WHERE distributor='EGD' AND code='custom_fve_home01'
                    ORDER BY valid_from DESC LIMIT 1),
    system_services_czk_kwh = 0.192,
    ote_fee_czk_kwh         = 0.001
WHERE site_id = (SELECT id FROM ems.site WHERE code='home-01');
