-- 1. Create the table if it doesn't exist
create table if not exists public.users (
  id uuid references auth.users on delete cascade not null primary key,
  email text,
  full_name text,
  avatar_url text,
  credits integer default 5,
  created_at timestamp with time zone default timezone('utc'::text, now()) not null
);

-- 2. Enable Row Level Security (RLS)
alter table public.users enable row level security;

-- 3. Create policies
-- Allow users to view their own profile
create policy "Users can view own profile" 
on public.users for select 
using (auth.uid() = id);

-- Allow users to insert their own profile
create policy "Users can insert own profile" 
on public.users for insert 
with check (auth.uid() = id);

-- Allow users to update their own profile
create policy "Users can update own profile" 
on public.users for update 
using (auth.uid() = id);
