<?php
session_start();
require_once '../config/database.php';

if (!isset($_SESSION["user_id"]) || $_SESSION["role"] !== "partner") {
    header("Location: ../auth/login.php");
    exit;
}

$error = "";

$uploadDir = __DIR__ . "/../uploads/logos/";
$uploadPathForDB = "";

if (!is_dir($uploadDir)) {
    mkdir($uploadDir, 0755, true);
}

if ($_SERVER["REQUEST_METHOD"] === "POST") {

    if (!isset($_FILES["logo"]) || $_FILES["logo"]["error"] !== UPLOAD_ERR_OK) {
        $error = "Please choose a valid image file.";
    } else {

        $allowedExtensions = ["jpg", "jpeg", "png", "webp"];
        $allowedMimeTypes = ["image/jpeg", "image/png", "image/webp"];
        $maxSize = 2 * 1024 * 1024; // 2MB

        $fileTmp = $_FILES["logo"]["tmp_name"];
        $fileSize = $_FILES["logo"]["size"];
        $fileName = $_FILES["logo"]["name"];

        $extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
        $mimeType = mime_content_type($fileTmp);

        if (!in_array($extension, $allowedExtensions)) {
            $error = "Only JPG, PNG, and WEBP files are allowed.";
        } elseif (!in_array($mimeType, $allowedMimeTypes)) {
            $error = "Invalid image type.";
        } elseif ($fileSize > $maxSize) {
            $error = "Logo must be smaller than 2MB.";
        } else {

            $newFileName = "logo_" . $_SESSION["user_id"] . "_" . time() . "." . $extension;
            $destination = $uploadDir . $newFileName;

            if (move_uploaded_file($fileTmp, $destination)) {

                $stmt = $pdo->prepare("
                    UPDATE partner_shops
                    SET logo = ?
                    WHERE user_id = ?
                ");

                $stmt->execute([$newFileName, $_SESSION["user_id"]]);

                header("Location: dashboard.php");
                exit;

            } else {
                $error = "Upload failed. Please check folder permission for uploads/logos.";
            }
        }
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Upload Shop Logo</title>
    <style>
        * {
            box-sizing: border-box;
        }

        body {
            margin: 0;
            font-family: Arial, sans-serif;
            background: linear-gradient(135deg, #eef2ff, #f8fafc);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 30px;
        }

        .upload-card {
            width: 100%;
            max-width: 480px;
            background: #ffffff;
            border-radius: 24px;
            padding: 34px;
            box-shadow: 0 20px 50px rgba(15, 23, 42, 0.12);
        }

        .logo-preview {
            width: 86px;
            height: 86px;
            border-radius: 22px;
            background: #eff6ff;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 34px;
            margin-bottom: 22px;
        }

        h2 {
            margin: 0;
            color: #111827;
            font-size: 28px;
        }

        .subtitle {
            color: #6b7280;
            margin: 10px 0 24px;
            line-height: 1.5;
        }

        .error {
            background: #fee2e2;
            color: #991b1b;
            padding: 13px 15px;
            border-radius: 12px;
            margin-bottom: 18px;
            font-size: 14px;
        }

        .upload-box {
            border: 2px dashed #c7d2fe;
            background: #f8fafc;
            border-radius: 18px;
            padding: 28px;
            text-align: center;
            cursor: pointer;
            transition: 0.2s;
            display: block;
        }

        .upload-box:hover {
            border-color: #2563eb;
            background: #eff6ff;
        }

        .upload-box input {
            display: none;
        }

        .upload-icon {
            font-size: 38px;
            margin-bottom: 10px;
        }

        .upload-title {
            font-weight: bold;
            color: #111827;
            margin-bottom: 6px;
        }

        .upload-note {
            color: #6b7280;
            font-size: 14px;
        }

        .actions {
            display: flex;
            gap: 12px;
            margin-top: 24px;
        }

        .btn, button {
            border: none;
            border-radius: 12px;
            padding: 13px 18px;
            font-size: 15px;
            font-weight: bold;
            cursor: pointer;
            text-decoration: none;
            text-align: center;
        }

        button {
            background: #2563eb;
            color: white;
            flex: 1;
        }

        button:hover {
            background: #1d4ed8;
        }

        .btn {
            background: #e5e7eb;
            color: #111827;
        }
    </style>
</head>
<body>

<div class="upload-card">

    <div class="logo-preview">🖼️</div>

    <h2>Upload Shop Logo</h2>

    <p class="subtitle">
        Choose your shop logo. It will appear on the top-left of your partner dashboard.
    </p>

    <?php if (!empty($error)): ?>
        <div class="error">
            <?php echo htmlspecialchars($error); ?>
        </div>
    <?php endif; ?>

    <form method="POST" enctype="multipart/form-data">

        <label class="upload-box">
            <input type="file" name="logo" accept="image/jpeg,image/png,image/webp" required>

            <div class="upload-icon">⬆️</div>
            <div class="upload-title">Click to choose logo</div>
            <div class="upload-note">JPG, PNG, or WEBP only. Max size 2MB.</div>
        </label>

        <div class="actions">
            <a href="dashboard.php" class="btn">Back</a>
            <button type="submit">Upload Logo</button>
        </div>

    </form>

</div>

<?php require __DIR__ . '/../config/auto_update.php'; ?>
</body>
</html>