
-- Company-only bank accounts (separate from association bank_accounts)
CREATE TABLE IF NOT EXISTS public.company_bank_accounts (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  account_name TEXT NOT NULL,
  account_number TEXT,
  routing_number TEXT,
  bank_name TEXT,
  account_type TEXT DEFAULT 'checking',
  account_category TEXT DEFAULT 'operating',
  current_balance NUMERIC DEFAULT 0,
  next_check_number INTEGER NOT NULL DEFAULT 1001,
  status TEXT NOT NULL DEFAULT 'active',
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

ALTER TABLE public.company_bank_accounts ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Staff can view company bank accounts"
  ON public.company_bank_accounts FOR SELECT
  USING (public.has_role(auth.uid(), 'admin'::public.app_role) OR public.has_role(auth.uid(), 'manager'::public.app_role));

CREATE POLICY "Staff can insert company bank accounts"
  ON public.company_bank_accounts FOR INSERT
  WITH CHECK (public.has_role(auth.uid(), 'admin'::public.app_role) OR public.has_role(auth.uid(), 'manager'::public.app_role));

CREATE POLICY "Staff can update company bank accounts"
  ON public.company_bank_accounts FOR UPDATE
  USING (public.has_role(auth.uid(), 'admin'::public.app_role) OR public.has_role(auth.uid(), 'manager'::public.app_role));

CREATE POLICY "Staff can delete company bank accounts"
  ON public.company_bank_accounts FOR DELETE
  USING (public.has_role(auth.uid(), 'admin'::public.app_role) OR public.has_role(auth.uid(), 'manager'::public.app_role));

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

-- Company checks (separate from association checks)
CREATE TABLE IF NOT EXISTS public.company_checks (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  company_bank_account_id UUID REFERENCES public.company_bank_accounts(id) ON DELETE SET NULL,
  payee TEXT NOT NULL,
  amount NUMERIC NOT NULL DEFAULT 0,
  check_number TEXT,
  memo TEXT,
  check_date DATE,
  status TEXT NOT NULL DEFAULT 'draft',
  printed BOOLEAN NOT NULL DEFAULT false,
  created_by UUID,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

ALTER TABLE public.company_checks ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Staff can view company checks"
  ON public.company_checks FOR SELECT
  USING (public.has_role(auth.uid(), 'admin'::public.app_role) OR public.has_role(auth.uid(), 'manager'::public.app_role));

CREATE POLICY "Staff can insert company checks"
  ON public.company_checks FOR INSERT
  WITH CHECK (public.has_role(auth.uid(), 'admin'::public.app_role) OR public.has_role(auth.uid(), 'manager'::public.app_role));

CREATE POLICY "Staff can update company checks"
  ON public.company_checks FOR UPDATE
  USING (public.has_role(auth.uid(), 'admin'::public.app_role) OR public.has_role(auth.uid(), 'manager'::public.app_role));

CREATE POLICY "Staff can delete company checks"
  ON public.company_checks FOR DELETE
  USING (public.has_role(auth.uid(), 'admin'::public.app_role) OR public.has_role(auth.uid(), 'manager'::public.app_role));

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

-- Link company bank transactions to a specific company account & optionally a check
ALTER TABLE public.company_bank_transactions
  ADD COLUMN IF NOT EXISTS company_bank_account_id UUID REFERENCES public.company_bank_accounts(id) ON DELETE SET NULL,
  ADD COLUMN IF NOT EXISTS check_id UUID REFERENCES public.company_checks(id) ON DELETE SET NULL;

CREATE INDEX IF NOT EXISTS idx_company_bank_transactions_account
  ON public.company_bank_transactions(company_bank_account_id);
CREATE INDEX IF NOT EXISTS idx_company_checks_account
  ON public.company_checks(company_bank_account_id);
