-- Database schema for the insole-production time tracker (apps/web backend).
--
-- This file was reverse-engineered from the API route queries — the original
-- Create/Anything export shipped no migration. Run it once against a fresh
-- Postgres database (Neon or local) to create the two application tables the
-- app needs:
--
--   psql "$DATABASE_URL" -f apps/web/db/schema.sql
--
-- Auth (better-auth user/session/account/verification tables) is NOT created
-- here. The core flow — tasks, stopwatch, history, CSV export — does not check
-- a session, so auth is optional for local development. If you do want sign-in,
-- generate those tables separately with: npx @better-auth/cli migrate

CREATE TABLE IF NOT EXISTS production_tasks (
  id           integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name         text NOT NULL,
  insole_types text[] NOT NULL DEFAULT ARRAY['Kurk', 'Berk', '3D']::text[],
  created_at   timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE IF NOT EXISTS time_logs (
  id               integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  task_id          integer NOT NULL REFERENCES production_tasks(id) ON DELETE CASCADE,
  start_time       timestamptz NOT NULL,
  end_time         timestamptz,
  duration_seconds integer NOT NULL DEFAULT 0,
  pair_count       integer NOT NULL DEFAULT 2,   -- "number of insoles" for the session
  insole_type      text,                          -- 'Kurk' | 'Berk' | '3D'
  notes            text,
  created_at       timestamptz NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS time_logs_task_id_idx    ON time_logs (task_id);
CREATE INDEX IF NOT EXISTS time_logs_start_time_idx ON time_logs (start_time DESC);
