-- Add activation/expiry to companies
ALTER TABLE public.companies
  ADD COLUMN IF NOT EXISTS activated_at timestamptz DEFAULT now(),
  ADD COLUMN IF NOT EXISTS expires_at timestamptz,
  ADD COLUMN IF NOT EXISTS address text;

-- Allow super admin to update companies
DROP POLICY IF EXISTS "Super admins can update companies" ON public.companies;
CREATE POLICY "Super admins can update companies"
ON public.companies FOR UPDATE
TO authenticated
USING (public.is_super_admin(auth.uid()))
WITH CHECK (public.is_super_admin(auth.uid()));

-- Set ganpatienterprisesratia@gmail.com as company_admin of Acme Accounting
INSERT INTO public.user_roles (user_id, role, company_id)
SELECT '14fabcfa-bdcf-4c70-8cb8-9696c425e84f'::uuid, 'company_admin'::app_role, '11111111-1111-1111-1111-111111111111'::uuid
WHERE NOT EXISTS (
  SELECT 1 FROM public.user_roles
  WHERE user_id = '14fabcfa-bdcf-4c70-8cb8-9696c425e84f'
    AND role = 'company_admin'
    AND company_id = '11111111-1111-1111-1111-111111111111'
);