-- Migration 094: Add membership status fields to companies
-- membership_status: active, resigned, suspended, exempt (independent from portal visibility 'status')
-- resignation_date: when company left the chamber
-- fee_included_in_parent: subsidiary whose fees are covered by parent company

ALTER TABLE companies ADD COLUMN IF NOT EXISTS membership_status VARCHAR(20) DEFAULT 'active';
ALTER TABLE companies ADD COLUMN IF NOT EXISTS resignation_date DATE;
ALTER TABLE companies ADD COLUMN IF NOT EXISTS fee_included_in_parent BOOLEAN DEFAULT FALSE;

-- Set membership_status='active' for all currently active companies
UPDATE companies SET membership_status = 'active' WHERE status = 'active' AND membership_status IS NULL;

-- Set membership_status='resigned' for known inactive companies that resigned
UPDATE companies SET membership_status = 'resigned' WHERE status = 'inactive' AND membership_status IS NULL;

-- === RESIGNATIONS ===

-- KORNIX (id=82): rezygnacja z końcem 2025
UPDATE companies SET membership_status = 'resigned', status = 'inactive' WHERE id = 82;

-- PZU TFI (id=100): rezygnacja 02.03.2026 (already inactive)
UPDATE companies SET membership_status = 'resigned', resignation_date = '2026-03-02' WHERE id = 100;

-- === SUBSIDIARIES (fee included in parent) ===

-- Pelmar (id=51) → P&P (id=84)
UPDATE companies SET parent_company_id = 84, fee_included_in_parent = TRUE WHERE id = 51;

-- Chopin Telewizja Kablowa (id=25) → TTM (id=92)
UPDATE companies SET parent_company_id = 92, fee_included_in_parent = TRUE WHERE id = 25;

-- === NEW COMPANIES: Resigned members ===

-- MACIEJ HAŁAS - rezygnacja
INSERT INTO companies (name, slug, status, membership_status, data_quality)
VALUES ('Maciej Hałas', 'maciej-halas', 'inactive', 'resigned', 'basic')
ON CONFLICT (slug) DO NOTHING;

-- MARKISOL - rezygnacja 30.01.2025
INSERT INTO companies (name, slug, status, membership_status, resignation_date, data_quality)
VALUES ('Markisol', 'markisol', 'inactive', 'resigned', '2025-01-30', 'basic')
ON CONFLICT (slug) DO NOTHING;

-- === NEW COMPANIES: Active subsidiaries ===

-- FRESH BIKE → Eura-Tech (id=34)
INSERT INTO companies (name, slug, status, membership_status, parent_company_id, fee_included_in_parent, data_quality)
VALUES ('Fresh Bike', 'fresh-bike', 'active', 'active', 34, TRUE, 'basic')
ON CONFLICT (slug) DO NOTHING;

-- JANTAR - centrum handlowe → Eura-Tech (id=34)
INSERT INTO companies (name, slug, status, membership_status, parent_company_id, fee_included_in_parent, data_quality)
VALUES ('Jantar - Centrum Handlowe', 'jantar-centrum-handlowe', 'active', 'active', 34, TRUE, 'basic')
ON CONFLICT (slug) DO NOTHING;

-- WW GLASS → Lenap Hale (id=83)
INSERT INTO companies (name, slug, status, membership_status, parent_company_id, fee_included_in_parent, data_quality)
VALUES ('WW Glass', 'ww-glass', 'active', 'active', 83, TRUE, 'basic')
ON CONFLICT (slug) DO NOTHING;

-- === PZU: Waive fees from April 2026 onwards ===
UPDATE membership_fees
SET status = 'waived', notes = 'Rezygnacja z członkostwa od 02.03.2026'
WHERE company_id = 100
  AND fee_year = 2026
  AND fee_month >= 4
  AND status IN ('pending', 'overdue');

-- Grant permissions
GRANT ALL ON TABLE companies TO nordabiz_app;
