-- Fase 1: perfiles, catálogo de ejercicios, rutinas, sesiones de entreno y evaluaciones corporales.

create table public.profiles (
  id uuid primary key references auth.users (id) on delete cascade,
  display_name text not null,
  gym_name text default 'Colossus',
  height_cm numeric,
  joined_gym_at date,
  created_at timestamptz not null default now()
);

create table public.exercises (
  id bigint generated always as identity primary key,
  name text not null,
  muscle_group text,
  machine_name text,
  created_at timestamptz not null default now()
);

create table public.routines (
  id bigint generated always as identity primary key,
  user_id uuid not null references auth.users (id) on delete cascade,
  name text not null,
  description text,
  is_active boolean not null default true,
  created_at timestamptz not null default now()
);

create table public.routine_exercises (
  id bigint generated always as identity primary key,
  routine_id bigint not null references public.routines (id) on delete cascade,
  exercise_id bigint not null references public.exercises (id),
  position int not null default 0,
  target_sets int not null default 4,
  target_reps int not null default 12,
  target_weight_kg numeric
);

create table public.workout_sessions (
  id bigint generated always as identity primary key,
  user_id uuid not null references auth.users (id) on delete cascade,
  routine_id bigint references public.routines (id),
  started_at timestamptz not null default now(),
  ended_at timestamptz,
  notes text
);

create table public.session_sets (
  id bigint generated always as identity primary key,
  session_id bigint not null references public.workout_sessions (id) on delete cascade,
  exercise_id bigint not null references public.exercises (id),
  set_number int not null,
  weight_kg numeric not null,
  reps int not null,
  created_at timestamptz not null default now()
);

create table public.body_evaluations (
  id bigint generated always as identity primary key,
  user_id uuid not null references auth.users (id) on delete cascade,
  measured_at timestamptz not null,
  weight_kg numeric,
  body_fat_pct numeric,
  skeletal_muscle_kg numeric,
  muscle_mass_kg numeric,
  muscle_mass_pct numeric,
  protein_kg numeric,
  water_pct numeric,
  bmi numeric,
  visceral_fat_grade numeric,
  bmr_kcal numeric,
  body_age numeric,
  whr numeric,
  source text default 'fitdays',
  created_at timestamptz not null default now()
);

-- Row Level Security: cada usuario solo ve/edita lo suyo.
alter table public.profiles enable row level security;
alter table public.routines enable row level security;
alter table public.routine_exercises enable row level security;
alter table public.workout_sessions enable row level security;
alter table public.session_sets enable row level security;
alter table public.body_evaluations enable row level security;

create policy "profiles: owner full access" on public.profiles
  for all using (auth.uid() = id) with check (auth.uid() = id);

create policy "routines: owner full access" on public.routines
  for all using (auth.uid() = user_id) with check (auth.uid() = user_id);

create policy "routine_exercises: owner full access" on public.routine_exercises
  for all using (
    exists (select 1 from public.routines r where r.id = routine_id and r.user_id = auth.uid())
  ) with check (
    exists (select 1 from public.routines r where r.id = routine_id and r.user_id = auth.uid())
  );

create policy "workout_sessions: owner full access" on public.workout_sessions
  for all using (auth.uid() = user_id) with check (auth.uid() = user_id);

create policy "session_sets: owner full access" on public.session_sets
  for all using (
    exists (select 1 from public.workout_sessions s where s.id = session_id and s.user_id = auth.uid())
  ) with check (
    exists (select 1 from public.workout_sessions s where s.id = session_id and s.user_id = auth.uid())
  );

create policy "body_evaluations: owner full access" on public.body_evaluations
  for all using (auth.uid() = user_id) with check (auth.uid() = user_id);

-- Catálogo de ejercicios: lectura pública para cualquier usuario autenticado.
alter table public.exercises enable row level security;

create policy "exercises: authenticated read" on public.exercises
  for select using (auth.role() = 'authenticated');

-- Semilla inicial con máquinas visibles en el gimnasio Colossus.
insert into public.exercises (name, muscle_group, machine_name) values
  ('Sentadilla en máquina Smith', 'pierna', 'Smith machine'),
  ('Prensa de piernas', 'pierna', 'Leg press'),
  ('Extensión de cuádriceps', 'pierna', 'Leg extension'),
  ('Curl femoral sentado', 'pierna', 'Seated leg curl'),
  ('Press de banca', 'pecho', 'Bench press'),
  ('Press de pecho en máquina', 'pecho', 'Chest press machine'),
  ('Jalón al pecho', 'espalda', 'Lat pulldown'),
  ('Remo sentado en polea', 'espalda', 'Seated cable row'),
  ('Press militar en máquina', 'hombro', 'Shoulder press machine'),
  ('Curl de bíceps con mancuernas', 'brazo', 'Dumbbell curl'),
  ('Extensión de tríceps en polea', 'brazo', 'Triceps pushdown');
