
-- Fee schedule subcategories
CREATE TABLE public.fee_schedule_subcategories (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  name TEXT NOT NULL,
  is_custom BOOLEAN DEFAULT true,
  created_at TIMESTAMPTZ DEFAULT now()
);

-- Fee schedules
CREATE TABLE public.fee_schedules (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  description TEXT NOT NULL,
  fee NUMERIC NOT NULL DEFAULT 0,
  category TEXT,
  subcategory TEXT,
  created_at TIMESTAMPTZ DEFAULT now(),
  updated_at TIMESTAMPTZ DEFAULT now()
);

ALTER TABLE public.fee_schedule_subcategories ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.fee_schedules ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Authenticated users can manage subcategories"
  ON public.fee_schedule_subcategories FOR ALL TO authenticated
  USING (true) WITH CHECK (true);

CREATE POLICY "Authenticated users can manage fee schedules"
  ON public.fee_schedules FOR ALL TO authenticated
  USING (true) WITH CHECK (true);

CREATE TRIGGER update_fee_schedules_updated_at
  BEFORE UPDATE ON public.fee_schedules
  FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

-- Seed default subcategories
INSERT INTO public.fee_schedule_subcategories (name, is_custom) VALUES
  ('Estoppel', false),
  ('Late Fee', false),
  ('Legal', false),
  ('Compliance', false),
  ('Administrative', false),
  ('Maintenance', false);
