create table "public"."organization_details" (
    "id" uuid not null default gen_random_uuid(),
    "created_at" timestamp with time zone not null default timezone('utc'::text, now()),
    "updated_at" timestamp with time zone not null default timezone('utc'::text, now()),
    "owner_id" uuid not null,
    "name" text not null,
    "organization_number" text not null,
    "organization_type" text not null default 'AS'::text,
    "mva_registered" boolean not null default false,
    "country_code" text not null default 'NO'::text,
    "currency" text not null default 'NOK'::text,
    "address_line_1" text,
    "address_line_2" text,
    "notes" text
);


alter table "public"."organization_details" enable row level security;

CREATE INDEX idx_organization_details_organization_number ON public.organization_details USING btree (organization_number) WHERE (organization_number IS NOT NULL);

CREATE INDEX idx_organization_details_owner_id ON public.organization_details USING btree (owner_id);

CREATE UNIQUE INDEX organization_details_pkey ON public.organization_details USING btree (id);

CREATE UNIQUE INDEX organization_details_unique_registration ON public.organization_details USING btree (organization_number, country_code);

alter table "public"."organization_details" add constraint "organization_details_pkey" PRIMARY KEY using index "organization_details_pkey";

alter table "public"."organization_details" add constraint "organization_details_country_code_format" CHECK ((country_code ~* '^[A-Z]{2}$'::text)) not valid;

alter table "public"."organization_details" validate constraint "organization_details_country_code_format";

alter table "public"."organization_details" add constraint "organization_details_name_length" CHECK (((char_length(name) >= 1) AND (char_length(name) <= 255))) not valid;

alter table "public"."organization_details" validate constraint "organization_details_name_length";

alter table "public"."organization_details" add constraint "organization_details_organization_number_format" CHECK (((organization_number IS NULL) OR ((country_code = 'NO'::text) AND (organization_number ~ '^[0-9]{9}$'::text)) OR (country_code <> 'NO'::text))) not valid;

alter table "public"."organization_details" validate constraint "organization_details_organization_number_format";

alter table "public"."organization_details" add constraint "organization_details_owner_id_fkey" FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE not valid;

alter table "public"."organization_details" validate constraint "organization_details_owner_id_fkey";

alter table "public"."organization_details" add constraint "organization_details_unique_registration" UNIQUE using index "organization_details_unique_registration";

set check_function_bodies = off;

CREATE OR REPLACE FUNCTION public.update_updated_at_column()
 RETURNS trigger
 LANGUAGE plpgsql
 SECURITY DEFINER
 SET search_path TO ''
AS $function$
begin
  new.updated_at = timezone('utc'::text, now());
  return new;
end;
$function$
;

grant delete on table "public"."organization_details" to "anon";

grant insert on table "public"."organization_details" to "anon";

grant references on table "public"."organization_details" to "anon";

grant select on table "public"."organization_details" to "anon";

grant trigger on table "public"."organization_details" to "anon";

grant truncate on table "public"."organization_details" to "anon";

grant update on table "public"."organization_details" to "anon";

grant delete on table "public"."organization_details" to "authenticated";

grant insert on table "public"."organization_details" to "authenticated";

grant references on table "public"."organization_details" to "authenticated";

grant select on table "public"."organization_details" to "authenticated";

grant trigger on table "public"."organization_details" to "authenticated";

grant truncate on table "public"."organization_details" to "authenticated";

grant update on table "public"."organization_details" to "authenticated";

grant delete on table "public"."organization_details" to "service_role";

grant insert on table "public"."organization_details" to "service_role";

grant references on table "public"."organization_details" to "service_role";

grant select on table "public"."organization_details" to "service_role";

grant trigger on table "public"."organization_details" to "service_role";

grant truncate on table "public"."organization_details" to "service_role";

grant update on table "public"."organization_details" to "service_role";

create policy "Users can create their own organization details"
on "public"."organization_details"
as permissive
for insert
to authenticated
with check ((( SELECT auth.uid() AS uid) = owner_id));


create policy "Users can delete their own organization details"
on "public"."organization_details"
as permissive
for delete
to authenticated
using ((( SELECT auth.uid() AS uid) = owner_id));


create policy "Users can update their own organization details"
on "public"."organization_details"
as permissive
for update
to authenticated
using ((( SELECT auth.uid() AS uid) = owner_id));


create policy "Users can view their own organization details"
on "public"."organization_details"
as permissive
for select
to authenticated
using ((( SELECT auth.uid() AS uid) = owner_id));


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


