-- Enable UUID extension
create extension if not exists "uuid-ossp";

-- Create Profiles Table (Extends Auth)
create table profiles (
  id uuid references auth.users on delete cascade not null primary key,
  role text check (role in ('admin', 'staff')) default 'staff',
  full_name text,
  created_at timestamp with time zone default timezone('utc'::text, now()) not null
);

-- Create Customers Table (CRM)
create table customers (
  id uuid default uuid_generate_v4() primary key,
  full_name text not null,
  phone text,
  email text,
  city text,
  district text,
  address text,
  notes text,
  created_at timestamp with time zone default timezone('utc'::text, now()) not null
);

-- Create Halls Table (Salons)
create table halls (
  id uuid default uuid_generate_v4() primary key,
  name text not null,
  capacity int,
  description text,
  features text[],
  created_at timestamp with time zone default timezone('utc'::text, now()) not null
);

-- Create Packages Table (Pricing)
create table packages (
  id uuid default uuid_generate_v4() primary key,
  name text not null,
  description text,
  price decimal(10,2) not null,
  is_active boolean default true,
  created_at timestamp with time zone default timezone('utc'::text, now()) not null
);

-- Create Reservations Table
create table reservations (
  id uuid default uuid_generate_v4() primary key,
  hall_id uuid references halls(id) on delete set null,
  customer_id uuid references customers(id) on delete set null,
  package_id uuid references packages(id) on delete set null,
  start_time timestamp with time zone not null,
  end_time timestamp with time zone not null,
  status text check (status in ('pending', 'confirmed', 'cancelled', 'completed')) default 'pending',
  notes text,
  created_by uuid references auth.users(id),
  created_at timestamp with time zone default timezone('utc'::text, now()) not null
);

-- Create Payments Table
create table payments (
  id uuid default uuid_generate_v4() primary key,
  reservation_id uuid references reservations(id) on delete cascade,
  amount decimal(10,2) not null,
  payment_type text check (payment_type in ('deposit', 'full', 'remaining')),
  payment_method text check (payment_method in ('cash', 'credit_card', 'transfer')),
  status text check (status in ('pending', 'paid', 'refunded')) default 'pending',
  paid_at timestamp with time zone,
  created_at timestamp with time zone default timezone('utc'::text, now()) not null
);

-- RLS Policies
alter table profiles enable row level security;
alter table customers enable row level security;
alter table halls enable row level security;
alter table packages enable row level security;
alter table reservations enable row level security;
alter table payments enable row level security;

create policy "Enable all access for authenticated users" on profiles for all using (auth.role() = 'authenticated');
create policy "Enable all access for authenticated users" on customers for all using (auth.role() = 'authenticated');
create policy "Enable all access for authenticated users" on halls for all using (auth.role() = 'authenticated');
create policy "Enable all access for authenticated users" on packages for all using (auth.role() = 'authenticated');
create policy "Enable all access for authenticated users" on reservations for all using (auth.role() = 'authenticated');
create policy "Enable all access for authenticated users" on payments for all using (auth.role() = 'authenticated');

create or replace function public.handle_new_user()
returns trigger as $$
begin
  insert into public.profiles (id, full_name, role)
  values (new.id, new.raw_user_meta_data->>'full_name', 'staff');
  return new;
end;
$$ language plpgsql security definer;

create trigger on_auth_user_created
  after insert on auth.users
  for each row execute procedure public.handle_new_user();

-- Create Audit Logs Table
create table audit_logs (
  id uuid default uuid_generate_v4() primary key,
  user_id uuid references auth.users(id),
  action text not null,
  entity_type text not null,
  entity_id uuid,
  details jsonb,
  created_at timestamp with time zone default timezone('utc'::text, now()) not null
);

-- RLS for Audit Logs
alter table audit_logs enable row level security;

create policy "Admins can read all logs" on audit_logs
  for select using (
    exists (
      select 1 from profiles
      where profiles.id = auth.uid() and profiles.role = 'admin'
    )
  );

create policy "Users can insert logs" on audit_logs
  for insert with check (auth.uid() = user_id);
