<?php
// This migration script should be included/run after bootstrap initializes the app.
// Do NOT require bootstrap here to avoid recursive includes when bootstrap requires this file.
use Tshen\Database;

Database::run('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT UNIQUE, password TEXT, role TEXT, credits INTEGER DEFAULT 0)');
Database::run('CREATE TABLE IF NOT EXISTS classes (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, location TEXT, date TEXT, time TEXT, capacity INTEGER)');
Database::run('CREATE TABLE IF NOT EXISTS bookings (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, class_id INTEGER, status TEXT)');
Database::run('CREATE TABLE IF NOT EXISTS credit_purchases (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, credits INTEGER, expires_at TEXT)');
Database::run('CREATE TABLE IF NOT EXISTS credit_codes (id INTEGER PRIMARY KEY AUTOINCREMENT, code TEXT UNIQUE, credits INTEGER, used_by TEXT)');
Database::run('CREATE TABLE IF NOT EXISTS products (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, price REAL)');
Database::run('CREATE TABLE IF NOT EXISTS discount_codes (id INTEGER PRIMARY KEY AUTOINCREMENT, code TEXT UNIQUE, percent INTEGER)');
Database::run('CREATE TABLE IF NOT EXISTS orders (id INTEGER PRIMARY KEY AUTOINCREMENT, product_id INTEGER, qty INTEGER, total_price REAL)');
Database::run('CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, email TEXT, message TEXT)');
// User profiles table for storing additional user info and profile pictures
Database::run('CREATE TABLE IF NOT EXISTS user_profiles (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER UNIQUE,
    first_name TEXT,
    last_name TEXT,
    phone TEXT,
    address TEXT,
    gender TEXT,
    bio TEXT,
    profile_picture TEXT
)');

// Ensure uploads/profiles directory exists and is writable
$uploadDir = __DIR__ . '/../public/uploads/profiles';
if (!is_dir($uploadDir)) {
    mkdir($uploadDir, 0777, true);
}

// Seed minimal data if empty
if (!Database::run('SELECT 1 FROM users LIMIT 1')->fetch()) {
    $hash = password_hash('password', PASSWORD_DEFAULT);
    Database::run('INSERT INTO users (email,password,role,credits) VALUES (?,?,?,?)', ['admin@tshen.local',$hash,'admin',50]);
    Database::run('INSERT INTO users (email,password,role,credits) VALUES (?,?,?,?)', ['studio@tshen.local',$hash,'studio',0]);
    Database::run('INSERT INTO users (email,password,role,credits) VALUES (?,?,?,?)', ['user@tshen.local',$hash,'user',5]);
}

if (!Database::run('SELECT 1 FROM classes LIMIT 1')->fetch()) {
    $classes = [
        ['HIIT', 'Central', '2025-10-15', '09:00', 12],
        ['Yoga', 'East', '2025-10-16', '18:00', 15],
        ['Spin', 'West', '2025-10-17', '07:30', 10]
    ];
    foreach ($classes as $c) {
        Database::run('INSERT INTO classes (title,location,date,time,capacity) VALUES (?,?,?,?,?)', $c);
    }
}

if (!Database::run('SELECT 1 FROM products LIMIT 1')->fetch()) {
    Database::run('INSERT INTO products (name,price) VALUES (?,?)', ['TSHEN Tee', 20]);
    Database::run('INSERT INTO products (name,price) VALUES (?,?)', ['TSHEN Bottle', 12]);
}

if (php_sapi_name() === 'cli') {
    echo "Migrations and seed completed\n";
}