-- company_settings already exists; ensure columns + policies are correct
CREATE TABLE IF NOT EXISTS public.company_settings (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  zoho_organization_id TEXT,
  company_name TEXT,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

ALTER TABLE public.company_settings ADD COLUMN IF NOT EXISTS zoho_organization_id TEXT;
ALTER TABLE public.company_settings ADD COLUMN IF NOT EXISTS company_name TEXT;

ALTER TABLE public.company_settings ENABLE ROW LEVEL SECURITY;

-- Company check layout
CREATE TABLE IF NOT EXISTS public.company_check_layouts (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  check_position TEXT NOT NULL DEFAULT 'top',
  offset_x NUMERIC NOT NULL DEFAULT 0,
  offset_y NUMERIC NOT NULL DEFAULT 0,
  show_payer_block BOOLEAN NOT NULL DEFAULT true,
  show_logo BOOLEAN NOT NULL DEFAULT true,
  payer_name TEXT,
  payer_address TEXT,
  show_signature_line BOOLEAN NOT NULL DEFAULT true,
  signature_image_url TEXT,
  signature_label TEXT DEFAULT 'Authorized Signature',
  memo_prefix TEXT,
  footer_text TEXT,
  show_field_labels BOOLEAN NOT NULL DEFAULT false,
  font_family TEXT NOT NULL DEFAULT 'helvetica',
  field_positions JSONB NOT NULL DEFAULT '{}'::jsonb,
  logo_url TEXT,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

ALTER TABLE public.company_check_layouts ENABLE ROW LEVEL SECURITY;

DROP POLICY IF EXISTS "Staff can view company check layouts" ON public.company_check_layouts;
CREATE POLICY "Staff can view company check layouts"
  ON public.company_check_layouts FOR SELECT
  USING (public.has_role(auth.uid(), 'admin'::public.app_role) OR public.has_role(auth.uid(), 'manager'::public.app_role));

DROP POLICY IF EXISTS "Staff can insert company check layouts" ON public.company_check_layouts;
CREATE POLICY "Staff can insert company check layouts"
  ON public.company_check_layouts FOR INSERT
  WITH CHECK (public.has_role(auth.uid(), 'admin'::public.app_role) OR public.has_role(auth.uid(), 'manager'::public.app_role));

DROP POLICY IF EXISTS "Staff can update company check layouts" ON public.company_check_layouts;
CREATE POLICY "Staff can update company check layouts"
  ON public.company_check_layouts FOR UPDATE
  USING (public.has_role(auth.uid(), 'admin'::public.app_role) OR public.has_role(auth.uid(), 'manager'::public.app_role));

DROP POLICY IF EXISTS "Staff can delete company check layouts" ON public.company_check_layouts;
CREATE POLICY "Staff can delete company check layouts"
  ON public.company_check_layouts FOR DELETE
  USING (public.has_role(auth.uid(), 'admin'::public.app_role) OR public.has_role(auth.uid(), 'manager'::public.app_role));

DROP TRIGGER IF EXISTS trg_company_check_layouts_updated ON public.company_check_layouts;
CREATE TRIGGER trg_company_check_layouts_updated
  BEFORE UPDATE ON public.company_check_layouts
  FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();