-- Create a table for public profiles
create table profiles (
  id uuid references auth.users on delete cascade not null primary key,
  updated_at timestamp with time zone,
  email text,
  full_name text,
  avatar_url text,
  provider text,

  constraint full_name_length check (char_length(full_name) >= 3 and char_length(full_name) <= 50)
);
-- Set up Row Level Security (RLS)
-- See https://supabase.com/docs/guides/auth/row-level-security for more details.
alter table profiles
  enable row level security;

create policy "Public profiles are viewable by everyone." on profiles
  for select using (true);

create policy "Users can insert their own profile." on profiles
  for insert with check ((select auth.uid()) = id);

create policy "Users can update own profile." on profiles
  for update using ((select auth.uid()) = id);

-- This trigger automatically creates a profile entry when a new user signs up via Supabase Auth.
-- See https://supabase.com/docs/guides/auth/managing-user-data#using-triggers for more details.
create function public.handle_new_user()
returns trigger
set search_path = ''
as $$
begin
  insert into public.profiles (id, email, full_name, avatar_url, provider, updated_at)
  values (
    new.id,
    new.email,
    new.raw_user_meta_data->>'full_name',
    new.raw_user_meta_data->>'avatar_url'
    new.raw_user_meta_data->>'provider',
    now()
  );
  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();

-- Set up Storage!
insert into storage.buckets (id, name)
  values ('avatars', 'avatars');

-- Set up access controls for storage.
-- See https://supabase.com/docs/guides/storage#policy-examples for more details.
create policy "Avatar images are publicly accessible." on storage.objects
  for select using (bucket_id = 'avatars');

create policy "Anyone can upload an avatar." on storage.objects
  for insert with check (bucket_id = 'avatars');


-- Create a table for public statuses
CREATE TABLE statuses (
  id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
  user_id uuid REFERENCES auth.users ON DELETE CASCADE NOT NULL,
  updated_by_id uuid REFERENCES auth.users ON DELETE SET NULL DEFAULT auth.uid(),
  created_at timestamp with time zone DEFAULT now() NOT NULL,
  status text NOT NULL,
  CONSTRAINT status_length CHECK (char_length(status) >= 3 AND char_length(status) <= 80),
  CONSTRAINT statuses_user_id_fkey FOREIGN KEY (user_id) REFERENCES profiles(id) ON DELETE CASCADE
);

-- Set up Row Level Security (RLS)
ALTER TABLE statuses
  ENABLE ROW LEVEL SECURITY;

-- Policies
CREATE POLICY "Public statuses are viewable by everyone." ON statuses
  FOR SELECT USING (true);

CREATE POLICY "Users can insert statuses for any user." ON statuses
  FOR INSERT WITH CHECK (auth.role() = 'authenticated');

-- Function to add first status
CREATE FUNCTION public.handle_first_status()
RETURNS TRIGGER
SET search_path = ''
AS $$
BEGIN
  INSERT INTO public.statuses (user_id, updated_by_id, status)
  VALUES (
    NEW.id,
    NEW.id,
    'Just joined!'
  );
  RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

-- Create a separate trigger for the status
CREATE TRIGGER on_auth_user_created_add_status
  AFTER INSERT ON auth.users
  FOR EACH ROW EXECUTE PROCEDURE public.handle_first_status();

alter publication supabase_realtime add table statuses;
