-- Budget/Finance and Sponsors & Partners modules
-- Adds new module types and creates tables for both

-- ============================================================
-- 1. Extend module_type enum
-- ============================================================
ALTER TYPE module_type ADD VALUE IF NOT EXISTS 'budget';
ALTER TYPE module_type ADD VALUE IF NOT EXISTS 'sponsors';

-- ============================================================
-- 2. Budget Categories
-- ============================================================
CREATE TABLE budget_categories (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  department_id UUID NOT NULL REFERENCES event_departments(id) ON DELETE CASCADE,
  name TEXT NOT NULL,
  color TEXT DEFAULT '#6366f1',
  sort_order INT NOT NULL DEFAULT 0,
  created_at TIMESTAMPTZ DEFAULT now()
);

CREATE INDEX idx_budget_categories_dept ON budget_categories(department_id);

-- ============================================================
-- 3. Budget Items (income or expense line items)
-- ============================================================
CREATE TABLE budget_items (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  department_id UUID NOT NULL REFERENCES event_departments(id) ON DELETE CASCADE,
  category_id UUID REFERENCES budget_categories(id) ON DELETE SET NULL,
  description TEXT NOT NULL,
  item_type TEXT NOT NULL DEFAULT 'expense' CHECK (item_type IN ('income', 'expense')),
  planned_amount NUMERIC(12,2) NOT NULL DEFAULT 0,
  actual_amount NUMERIC(12,2) NOT NULL DEFAULT 0,
  notes TEXT,
  sort_order INT NOT NULL DEFAULT 0,
  created_by UUID REFERENCES auth.users(id) ON DELETE SET NULL,
  created_at TIMESTAMPTZ DEFAULT now(),
  updated_at TIMESTAMPTZ DEFAULT now()
);

CREATE INDEX idx_budget_items_dept ON budget_items(department_id);
CREATE INDEX idx_budget_items_category ON budget_items(category_id);

-- ============================================================
-- 4. Sponsor Tiers (e.g. Platinum, Gold, Silver)
-- ============================================================
CREATE TABLE sponsor_tiers (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  department_id UUID NOT NULL REFERENCES event_departments(id) ON DELETE CASCADE,
  name TEXT NOT NULL,
  amount NUMERIC(12,2) DEFAULT 0,
  color TEXT DEFAULT '#F59E0B',
  sort_order INT NOT NULL DEFAULT 0,
  created_at TIMESTAMPTZ DEFAULT now()
);

CREATE INDEX idx_sponsor_tiers_dept ON sponsor_tiers(department_id);

-- ============================================================
-- 5. Sponsors
-- ============================================================
CREATE TABLE sponsors (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  department_id UUID NOT NULL REFERENCES event_departments(id) ON DELETE CASCADE,
  tier_id UUID REFERENCES sponsor_tiers(id) ON DELETE SET NULL,
  name TEXT NOT NULL,
  contact_name TEXT,
  contact_email TEXT,
  contact_phone TEXT,
  website TEXT,
  logo_url TEXT,
  status TEXT NOT NULL DEFAULT 'prospect' CHECK (status IN ('prospect', 'contacted', 'confirmed', 'declined', 'active')),
  amount NUMERIC(12,2) DEFAULT 0,
  notes TEXT,
  created_by UUID REFERENCES auth.users(id) ON DELETE SET NULL,
  created_at TIMESTAMPTZ DEFAULT now(),
  updated_at TIMESTAMPTZ DEFAULT now()
);

CREATE INDEX idx_sponsors_dept ON sponsors(department_id);
CREATE INDEX idx_sponsors_tier ON sponsors(tier_id);
CREATE INDEX idx_sponsors_status ON sponsors(status);

-- ============================================================
-- 6. Sponsor Deliverables (what we owe each sponsor)
-- ============================================================
CREATE TABLE sponsor_deliverables (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  sponsor_id UUID NOT NULL REFERENCES sponsors(id) ON DELETE CASCADE,
  description TEXT NOT NULL,
  is_completed BOOLEAN NOT NULL DEFAULT false,
  due_date TIMESTAMPTZ,
  sort_order INT NOT NULL DEFAULT 0,
  created_at TIMESTAMPTZ DEFAULT now(),
  updated_at TIMESTAMPTZ DEFAULT now()
);

CREATE INDEX idx_sponsor_deliverables_sponsor ON sponsor_deliverables(sponsor_id);

-- ============================================================
-- 7. RLS Policies
-- ============================================================
ALTER TABLE budget_categories ENABLE ROW LEVEL SECURITY;
ALTER TABLE budget_items ENABLE ROW LEVEL SECURITY;
ALTER TABLE sponsor_tiers ENABLE ROW LEVEL SECURITY;
ALTER TABLE sponsors ENABLE ROW LEVEL SECURITY;
ALTER TABLE sponsor_deliverables ENABLE ROW LEVEL SECURITY;

-- Budget Categories
CREATE POLICY "Org members can view budget categories" ON budget_categories FOR SELECT
  USING (EXISTS (
    SELECT 1 FROM event_departments ed
    JOIN events e ON ed.event_id = e.id
    JOIN org_members om ON e.org_id = om.org_id
    WHERE ed.id = budget_categories.department_id AND om.user_id = auth.uid()
  ));

CREATE POLICY "Editors can manage budget categories" ON budget_categories FOR ALL
  USING (EXISTS (
    SELECT 1 FROM event_departments ed
    JOIN events e ON ed.event_id = e.id
    JOIN org_members om ON e.org_id = om.org_id
    WHERE ed.id = budget_categories.department_id AND om.user_id = auth.uid() AND om.role IN ('owner', 'admin', 'editor')
  ));

-- Budget Items
CREATE POLICY "Org members can view budget items" ON budget_items FOR SELECT
  USING (EXISTS (
    SELECT 1 FROM event_departments ed
    JOIN events e ON ed.event_id = e.id
    JOIN org_members om ON e.org_id = om.org_id
    WHERE ed.id = budget_items.department_id AND om.user_id = auth.uid()
  ));

CREATE POLICY "Editors can manage budget items" ON budget_items FOR ALL
  USING (EXISTS (
    SELECT 1 FROM event_departments ed
    JOIN events e ON ed.event_id = e.id
    JOIN org_members om ON e.org_id = om.org_id
    WHERE ed.id = budget_items.department_id AND om.user_id = auth.uid() AND om.role IN ('owner', 'admin', 'editor')
  ));

-- Sponsor Tiers
CREATE POLICY "Org members can view sponsor tiers" ON sponsor_tiers FOR SELECT
  USING (EXISTS (
    SELECT 1 FROM event_departments ed
    JOIN events e ON ed.event_id = e.id
    JOIN org_members om ON e.org_id = om.org_id
    WHERE ed.id = sponsor_tiers.department_id AND om.user_id = auth.uid()
  ));

CREATE POLICY "Editors can manage sponsor tiers" ON sponsor_tiers FOR ALL
  USING (EXISTS (
    SELECT 1 FROM event_departments ed
    JOIN events e ON ed.event_id = e.id
    JOIN org_members om ON e.org_id = om.org_id
    WHERE ed.id = sponsor_tiers.department_id AND om.user_id = auth.uid() AND om.role IN ('owner', 'admin', 'editor')
  ));

-- Sponsors
CREATE POLICY "Org members can view sponsors" ON sponsors FOR SELECT
  USING (EXISTS (
    SELECT 1 FROM event_departments ed
    JOIN events e ON ed.event_id = e.id
    JOIN org_members om ON e.org_id = om.org_id
    WHERE ed.id = sponsors.department_id AND om.user_id = auth.uid()
  ));

CREATE POLICY "Editors can manage sponsors" ON sponsors FOR ALL
  USING (EXISTS (
    SELECT 1 FROM event_departments ed
    JOIN events e ON ed.event_id = e.id
    JOIN org_members om ON e.org_id = om.org_id
    WHERE ed.id = sponsors.department_id AND om.user_id = auth.uid() AND om.role IN ('owner', 'admin', 'editor')
  ));

-- Sponsor Deliverables
CREATE POLICY "Org members can view sponsor deliverables" ON sponsor_deliverables FOR SELECT
  USING (EXISTS (
    SELECT 1 FROM sponsors s
    JOIN event_departments ed ON s.department_id = ed.id
    JOIN events e ON ed.event_id = e.id
    JOIN org_members om ON e.org_id = om.org_id
    WHERE s.id = sponsor_deliverables.sponsor_id AND om.user_id = auth.uid()
  ));

CREATE POLICY "Editors can manage sponsor deliverables" ON sponsor_deliverables FOR ALL
  USING (EXISTS (
    SELECT 1 FROM sponsors s
    JOIN event_departments ed ON s.department_id = ed.id
    JOIN events e ON ed.event_id = e.id
    JOIN org_members om ON e.org_id = om.org_id
    WHERE s.id = sponsor_deliverables.sponsor_id AND om.user_id = auth.uid() AND om.role IN ('owner', 'admin', 'editor')
  ));

-- ============================================================
-- 8. Enable realtime
-- ============================================================
ALTER PUBLICATION supabase_realtime ADD TABLE budget_items;
ALTER PUBLICATION supabase_realtime ADD TABLE sponsors;
ALTER PUBLICATION supabase_realtime ADD TABLE sponsor_deliverables;
