<?php
require_once dirname(__DIR__) . '/config/config.php';
require_once dirname(__DIR__) . '/includes/functions.php';
startSecureSession();

if (isCustomerLoggedIn()) { header('Location: /customer/dashboard.php'); exit; }

$pageTitle = 'Customer Registration';
$errors = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name    = sanitize($_POST['contact_name'] ?? '');
    $company = sanitize($_POST['company_name'] ?? '');
    $email   = trim($_POST['email'] ?? '');
    $phone   = sanitize($_POST['phone'] ?? '');
    $pass    = $_POST['password'] ?? '';
    $confirm = $_POST['confirm_password'] ?? '';

    if (!$name || !$email || !$pass) {
        $errors[] = 'Name, email and password are required.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors[] = 'Invalid email address.';
    } elseif (strlen($pass) < 8) {
        $errors[] = 'Password must be at least 8 characters.';
    } elseif ($pass !== $confirm) {
        $errors[] = 'Passwords do not match.';
    } elseif (Database::fetchOne("SELECT id FROM customers WHERE email=?", [$email])) {
        $errors[] = 'An account with this email already exists. Please log in.';
    } else {
        $hash = password_hash($pass, PASSWORD_BCRYPT);
        $id = Database::insert(
            "INSERT INTO customers (company_name, contact_name, email, password, phone) VALUES (?,?,?,?,?)",
            [$company ?: $name, $name, $email, $hash, $phone]
        );
        $_SESSION['customer_id'] = $id;
        $_SESSION['customer_name'] = $name;
        logActivity('customer', $id, 'Registration');
        header('Location: /customer/dashboard.php');
        exit;
    }
}

include dirname(__DIR__) . '/includes/header.php';
?>

<div class="min-h-[80vh] flex items-center justify-center px-4 py-10">
  <div class="w-full max-w-md">
    <div class="text-center mb-8">
      <div class="text-2xl font-black font-display mb-1"><span class="text-orange-500">RIG</span> N <span class="text-orange-500">DIG</span></div>
      <h1 class="text-xl font-bold">Create Account</h1>
      <p class="text-sm text-gray-400 mt-1">Access your quotes, orders and invoices</p>
    </div>

    <?php foreach ($errors as $e): ?>
    <div class="bg-red-900 border border-red-600 text-red-200 rounded p-3 text-sm mb-4"><?= e($e) ?></div>
    <?php endforeach; ?>

    <div class="card-dark p-6">
      <form method="POST" class="space-y-4">
        <div>
          <label class="text-xs text-gray-400 block mb-1.5">Full Name *</label>
          <input type="text" name="contact_name" required value="<?= e($_POST['contact_name'] ?? '') ?>">
        </div>
        <div>
          <label class="text-xs text-gray-400 block mb-1.5">Company Name</label>
          <input type="text" name="company_name" value="<?= e($_POST['company_name'] ?? '') ?>">
        </div>
        <div>
          <label class="text-xs text-gray-400 block mb-1.5">Email *</label>
          <input type="email" name="email" required value="<?= e($_POST['email'] ?? '') ?>">
        </div>
        <div>
          <label class="text-xs text-gray-400 block mb-1.5">Phone</label>
          <input type="tel" name="phone" value="<?= e($_POST['phone'] ?? '') ?>">
        </div>
        <div>
          <label class="text-xs text-gray-400 block mb-1.5">Password * (min 8 characters)</label>
          <input type="password" name="password" required minlength="8">
        </div>
        <div>
          <label class="text-xs text-gray-400 block mb-1.5">Confirm Password *</label>
          <input type="password" name="confirm_password" required>
        </div>
        <button type="submit" class="btn-primary w-full py-3">Create Account →</button>
      </form>
      <div class="text-center mt-4 text-sm text-gray-400">
        Already have an account? <a href="/customer/login.php" class="text-orange-500 hover:underline">Sign in</a>
      </div>
    </div>
  </div>
</div>

<?php include dirname(__DIR__) . '/includes/footer.php'; ?>
