<?php
declare(strict_types=1);

/*
|--------------------------------------------------------------------------
| WesleyTunes Admin Seeder
|--------------------------------------------------------------------------
| Upload this file to:
| public_html/wesleytunes/public/seed_admin.php
|
| Open in browser:
| https://documinegh.com/wesleytunes/seed_admin.php
|
| IMPORTANT:
| Delete this file immediately after the admin account is created.
|--------------------------------------------------------------------------
*/

require_once __DIR__ . '/../config/app.php';

$config = require ROOT_PATH . '/config/database.php';

$adminName = 'WesleyTunes Admin';
$adminEmail = 'admin@wesleytunes.com';
$adminPassword = 'Admin@2026';

try {
    $dsn = "mysql:host={$config['host']};dbname={$config['database']};charset={$config['charset']}";

    $pdo = new PDO($dsn, $config['username'], $config['password'], [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES => false,
    ]);

    $passwordHash = password_hash($adminPassword, PASSWORD_DEFAULT);

    $check = $pdo->prepare("SELECT id FROM admins WHERE email = :email LIMIT 1");
    $check->execute([
        ':email' => $adminEmail,
    ]);

    $existingAdmin = $check->fetch();

    if ($existingAdmin) {
        $update = $pdo->prepare("
            UPDATE admins 
            SET 
                name = :name,
                password = :password,
                is_active = 1,
                updated_at = NOW()
            WHERE email = :email
            LIMIT 1
        ");

        $update->execute([
            ':name' => $adminName,
            ':password' => $passwordHash,
            ':email' => $adminEmail,
        ]);

        $message = 'Admin account already existed, so it has been updated successfully.';
    } else {
        $insert = $pdo->prepare("
            INSERT INTO admins 
                (name, email, password, is_active, created_at, updated_at)
            VALUES 
                (:name, :email, :password, 1, NOW(), NOW())
        ");

        $insert->execute([
            ':name' => $adminName,
            ':email' => $adminEmail,
            ':password' => $passwordHash,
        ]);

        $message = 'Admin account has been created successfully.';
    }

    echo '<!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>WesleyTunes Admin Seeder</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <style>
            body {
                margin: 0;
                min-height: 100vh;
                display: grid;
                place-items: center;
                font-family: Arial, sans-serif;
                background: #f2d36b;
                color: #0f172a;
            }
            .box {
                width: min(92%, 520px);
                background: #ffffff;
                border-radius: 24px;
                padding: 28px;
                box-shadow: 0 20px 60px rgba(0,0,0,.12);
                border: 1px solid #d3af47;
            }
            h2 {
                margin: 0 0 10px;
                color: #0d4fae;
            }
            p {
                line-height: 1.6;
                color: #475569;
            }
            .details {
                background: #f8fafc;
                border-radius: 16px;
                padding: 16px;
                margin-top: 18px;
                border: 1px solid #e2e8f0;
            }
            .details p {
                margin: 8px 0;
            }
            .details strong {
                color: #111827;
            }
            .danger {
                margin-top: 18px;
                padding: 14px;
                border-radius: 14px;
                background: #fee2e2;
                color: #991b1b;
                font-size: 14px;
            }
            a {
                display: inline-block;
                margin-top: 18px;
                background: #0d4fae;
                color: white;
                text-decoration: none;
                padding: 12px 18px;
                border-radius: 14px;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <h2>Seeder Completed</h2>
            <p>' . htmlspecialchars($message, ENT_QUOTES, 'UTF-8') . '</p>

            <div class="details">
                <p><strong>Email:</strong> ' . htmlspecialchars($adminEmail, ENT_QUOTES, 'UTF-8') . '</p>
                <p><strong>Password:</strong> ' . htmlspecialchars($adminPassword, ENT_QUOTES, 'UTF-8') . '</p>
            </div>

            <div class="danger">
                Please delete <strong>public/seed_admin.php</strong> immediately after this.
            </div>

            <a href="' . APP_URL . '/admin/login">Go to Admin Login</a>
        </div>
    </body>
    </html>';

} catch (Throwable $e) {
    http_response_code(500);

    echo '<h2>Seeder Error</h2>';
    echo '<p>' . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8') . '</p>';
}