-- IRON/OS cloud sync schema. Optional: the app works without it.
-- Run the whole file once in the SQL editor of your own free Supabase project.
-- The programme itself lives in the app file; these tables hold only what you did.
-- Every table is prefixed ironos_ to match the app's tablePrefix setting.

-- Every set actually performed. Standalone rows (no foreign keys) so offline sync stays trivial.
create table public.ironos_set_logs (
  id uuid primary key default gen_random_uuid(),
  client_key text unique not null,          -- idempotency key generated by the app
  session_date date not null,
  day_id int not null check (day_id between 1 and 7),
  mode text not null check (mode in ('deficit','maintenance','surplus')),
  exercise_id text not null,
  set_number int not null,
  weight_kg numeric,                        -- null for reps-only and timed movements
  reps int,                                 -- seconds for timed movements
  logged_at timestamptz not null default now()
);
create index ironos_set_logs_exercise_date on public.ironos_set_logs (exercise_id, session_date);

-- One row per finished session: the check-in data the coach reads.
create table public.ironos_sessions (
  id uuid primary key default gen_random_uuid(),
  client_key text unique not null,
  session_date date not null,
  day_id int not null check (day_id between 1 and 7),
  mode text not null check (mode in ('deficit','maintenance','surplus')),
  sleep_quality int check (sleep_quality between 1 and 5),
  energy int check (energy between 1 and 5),
  -- Generic "watch" score for the injury-adjacent area named by the app's watchLabel
  -- (elbow, knee, shoulder ...). 0 = nothing, 10 = worst. Column name kept for
  -- compatibility with the app; the label is set in the app config.
  elbow_during int check (elbow_during between 0 and 10),
  notes text,
  completed_at timestamptz not null default now()
);

-- Next-day response of the watched area, the signal that actually matters.
-- Kept separate from sessions because it is logged the day AFTER training.
create table public.ironos_elbow_log (
  id uuid primary key default gen_random_uuid(),
  client_key text unique not null,
  log_date date not null,
  refers_to_session_date date,
  -- Generic "watch" score, same scale as elbow_during above. Column name kept for
  -- compatibility with the app; the area it refers to is the app's watchLabel.
  next_day_response int not null check (next_day_response between 0 and 10),
  notes text,
  logged_at timestamptz not null default now()
);

create table public.ironos_bodyweight_log (
  id uuid primary key default gen_random_uuid(),
  client_key text unique not null,
  log_date date not null,
  weight_kg numeric not null,
  logged_at timestamptz not null default now()
);

-- Small key/value store: current mode, phase start date, per-exercise rest overrides,
-- and the coach_note row the app shows on its next open.
create table public.ironos_app_state (
  key text primary key,
  value jsonb not null,
  updated_at timestamptz not null default now()
);

-- Single-user personal app: RLS on with permissive policies for the publishable key.
-- Fine for one person's training log. Never share one project between two people.
alter table public.ironos_set_logs enable row level security;
alter table public.ironos_sessions enable row level security;
alter table public.ironos_elbow_log enable row level security;
alter table public.ironos_bodyweight_log enable row level security;
alter table public.ironos_app_state enable row level security;

create policy anon_all_set_logs   on public.ironos_set_logs       for all using (true) with check (true);
create policy anon_all_sessions   on public.ironos_sessions       for all using (true) with check (true);
create policy anon_all_elbow      on public.ironos_elbow_log      for all using (true) with check (true);
create policy anon_all_bodyweight on public.ironos_bodyweight_log for all using (true) with check (true);
create policy anon_all_app_state  on public.ironos_app_state      for all using (true) with check (true);
