-- ============================================================
-- TopTran — PostgreSQL table creation script
-- ============================================================
-- NOTE: The "users" table already exists with the schema below.
-- Do NOT run the commented block if it already exists.
-- ============================================================

-- [EXISTING TABLE — DO NOT RECREATE]
-- CREATE TABLE IF NOT EXISTS users (
--   id          TEXT        PRIMARY KEY,
--   name        TEXT        NOT NULL,
--   email       TEXT        NOT NULL UNIQUE,
--   password    TEXT        NOT NULL,
--   "createdAt" TIMESTAMPTZ DEFAULT NOW(),
--   "updatedAt" TIMESTAMPTZ
-- );

-- ============================================================
-- Enums
-- ============================================================
DO $$ BEGIN
  CREATE TYPE "TokenType" AS ENUM ('REFRESH');
EXCEPTION WHEN duplicate_object THEN NULL;
END $$;

-- ============================================================
-- Tokens
-- ============================================================
CREATE TABLE IF NOT EXISTS tokens (
  id          TEXT        PRIMARY KEY,
  token       TEXT        NOT NULL UNIQUE,
  type        "TokenType" NOT NULL,
  "userId"    TEXT        NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  "expiresAt" TIMESTAMPTZ NOT NULL,
  "createdAt" TIMESTAMPTZ DEFAULT NOW()
);

-- ============================================================
-- Companies
-- ============================================================
CREATE TABLE IF NOT EXISTS companies (
  id              TEXT          PRIMARY KEY,
  name            TEXT          NOT NULL,
  cost_per_km     NUMERIC(10,2) NOT NULL,
  departures      INTEGER       DEFAULT 0,
  failed_service  INTEGER       DEFAULT 0,
  idle_hours      NUMERIC(10,2) DEFAULT 0,
  notes           TEXT          DEFAULT '',
  "createdAt"     TIMESTAMPTZ   DEFAULT NOW(),
  "updatedAt"     TIMESTAMPTZ   DEFAULT NOW()
);

ALTER TABLE companies ADD COLUMN IF NOT EXISTS departures     INTEGER       DEFAULT 0;
ALTER TABLE companies ADD COLUMN IF NOT EXISTS failed_service INTEGER       DEFAULT 0;
ALTER TABLE companies ADD COLUMN IF NOT EXISTS idle_hours     NUMERIC(10,2) DEFAULT 0;

-- ============================================================
-- Rides
-- ============================================================
CREATE TABLE IF NOT EXISTS rides (
  id              TEXT          PRIMARY KEY,
  user_id         TEXT          NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  company         TEXT          NOT NULL,
  km              NUMERIC(10,2) NOT NULL,
  cost_per_km     NUMERIC(10,2) NOT NULL,
  total           NUMERIC(10,2) NOT NULL,
  ride_date       TEXT          NOT NULL,
  departures      INTEGER       DEFAULT 0,
  failed_service  INTEGER       DEFAULT 0,
  idle_hours      NUMERIC(10,2) DEFAULT 0,
  synced          SMALLINT      DEFAULT 0,
  "createdAt"     TIMESTAMPTZ   DEFAULT NOW(),
  "updatedAt"     TIMESTAMPTZ   DEFAULT NOW()
);

-- ============================================================
-- Migrations: add new columns if table already exists
-- ============================================================
ALTER TABLE rides ADD COLUMN IF NOT EXISTS departures     INTEGER       DEFAULT 0;
ALTER TABLE rides ADD COLUMN IF NOT EXISTS failed_service INTEGER       DEFAULT 0;
ALTER TABLE rides ADD COLUMN IF NOT EXISTS idle_hours     NUMERIC(10,2) DEFAULT 0;

-- ============================================================
-- Indexes
-- ============================================================
CREATE INDEX IF NOT EXISTS idx_rides_user_id   ON rides(user_id);
CREATE INDEX IF NOT EXISTS idx_rides_synced    ON rides(synced);
CREATE INDEX IF NOT EXISTS idx_companies_name  ON companies(name);
CREATE INDEX IF NOT EXISTS idx_tokens_user_id  ON tokens("userId");
