<?php

try {
    $pdo = new PDO('mysql:host=127.0.0.1;dbname=kenyan_pos', 'root', '');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Database connection successful\n";
    
    // Create tables table
    $sql = "CREATE TABLE IF NOT EXISTS tables (
        id bigint unsigned NOT NULL AUTO_INCREMENT,
        branch_id bigint unsigned NOT NULL,
        name varchar(255) NOT NULL,
        section enum('dining','bar','outdoor','vip','private') NOT NULL DEFAULT 'dining',
        capacity int NOT NULL DEFAULT 4,
        status enum('available','occupied','reserved','maintenance') NOT NULL DEFAULT 'available',
        is_active tinyint(1) DEFAULT 1,
        created_at timestamp NULL DEFAULT NULL,
        updated_at timestamp NULL DEFAULT NULL,
        PRIMARY KEY (id),
        KEY tables_branch_id_foreign (branch_id)
    )";
    
    $pdo->exec($sql);
    echo "Tables table created successfully\n";
    
    // Insert sample tables
    $tables = [
        ['branch_id' => 1, 'name' => 'Table 1', 'section' => 'dining', 'capacity' => 4],
        ['branch_id' => 1, 'name' => 'Table 2', 'section' => 'dining', 'capacity' => 6],
        ['branch_id' => 1, 'name' => 'Table 3', 'section' => 'dining', 'capacity' => 2],
        ['branch_id' => 1, 'name' => 'Bar 1', 'section' => 'bar', 'capacity' => 8],
        ['branch_id' => 1, 'name' => 'Bar 2', 'section' => 'bar', 'capacity' => 6],
        ['branch_id' => 1, 'name' => 'Outdoor 1', 'section' => 'outdoor', 'capacity' => 4],
    ];
    
    foreach ($tables as $table) {
        $pdo->exec("INSERT INTO tables (branch_id, name, section, capacity, created_at, updated_at) 
                    VALUES ({$table['branch_id']}, '{$table['name']}', '{$table['section']}', {$table['capacity']}, NOW(), NOW())");
    }
    
    echo "Sample tables inserted successfully\n";
    
} catch(Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}