-- ============================================================
-- BharathTrucks — App Users Migration (Username + Password)
-- Run in Supabase SQL Editor
-- ============================================================

CREATE TABLE IF NOT EXISTS app_users (
  id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
  username TEXT UNIQUE NOT NULL,
  name TEXT NOT NULL,
  password_hash TEXT NOT NULL,
  role TEXT NOT NULL CHECK (role IN ('driver', 'shipper', 'broker', 'admin')),
  phone TEXT,
  city TEXT,
  state TEXT,
  is_verified BOOLEAN DEFAULT FALSE,
  is_premium BOOLEAN DEFAULT FALSE,
  is_active BOOLEAN DEFAULT TRUE,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_app_users_role ON app_users(role);
CREATE INDEX IF NOT EXISTS idx_app_users_username ON app_users(username);

-- RLS
ALTER TABLE app_users ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Allow all on app_users" ON app_users FOR ALL USING (true) WITH CHECK (true);

-- ============================================================
-- Done! No Supabase Auth needed — app manages its own users.
-- ============================================================
