<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/middleware.php';

// Ensure user is a customer
require_customer();

// Initialize $pdo if not already set
if (!isset($pdo)) {
    $pdo = require_once __DIR__ . '/../config/database.php';
}

$meta = ['title' => 'Trademark Registration - Step ' . ($currentStep ?? 1) . ' | Go Legal'];
require_once __DIR__ . '/../includes/header.php';

// Initialize session if not started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Get current step from URL
$currentStep = $_GET['step'] ?? 1;
$currentStep = (int)$currentStep;

// Validate step range
if ($currentStep < 1 || $currentStep > 10) {
    $currentStep = 1;
}

// Initialize form data in session
if (!isset($_SESSION['trademark_form'])) {
    $_SESSION['trademark_form'] = [
        'step' => 1,
        'data' => [],
        'files' => []
    ];
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    handleFormSubmission($currentStep);
}

function handleFormSubmission(int $step): void {
    $data = $_POST;
    $files = $_FILES;
    
    // Validate and sanitize input
    $validatedData = validateStepData($step, $data);
    
    // Handle file uploads
    if (!empty($files)) {
        $uploadedFiles = handleFileUploads($step, $files);
        $_SESSION['trademark_form']['files'] = array_merge($_SESSION['trademark_form']['files'], $uploadedFiles);
    }
    
    // Save to session
    $_SESSION['trademark_form']['data'][$step] = $validatedData;
    $_SESSION['trademark_form']['step'] = $step;
    
    // Redirect to next step or save
    if (isset($data['save_later'])) {
        header('Location: /customer/dashboard.php');
        exit;
    } elseif (isset($data['next_step'])) {
        $nextStep = $step + 1;
        if ($nextStep <= 10) {
            header("Location: /customer/trademark-application.php?step=$nextStep");
            exit;
        }
    }
}

function validateStepData(int $step, array $data): array {
    $validated = [];
    
    switch ($step) {
        case 1:
            $validated = [
                'trademark_type' => $data['trademark_type'] ?? '',
                'trademark_name' => sanitizeInput($data['trademark_name'] ?? ''),
                'logo_description' => sanitizeInput($data['logo_description'] ?? '')
            ];
            break;
            
        case 2:
            $validated = [
                'owner_name' => sanitizeInput($data['owner_name'] ?? ''),
                'business_type' => $data['business_type'] ?? '',
                'country' => sanitizeInput($data['country'] ?? ''),
                'state' => sanitizeInput($data['state'] ?? ''),
                'city' => sanitizeInput($data['city'] ?? ''),
                'business_address' => sanitizeInput($data['business_address'] ?? ''),
                'email' => filter_var($data['email'] ?? '', FILTER_VALIDATE_EMAIL) ? $data['email'] : '',
                'phone' => sanitizeInput($data['phone'] ?? '')
            ];
            break;
            
        case 3:
            $validated = [
                'business_description' => sanitizeInput($data['business_description'] ?? '')
            ];
            break;
            
        case 4:
            $validated = [
                'product_categories' => $data['product_categories'] ?? [],
                'products_services_description' => sanitizeInput($data['products_services_description'] ?? '')
            ];
            break;
            
        case 5:
            $validated = [
                'preferred_class' => sanitizeInput($data['preferred_class'] ?? '')
            ];
            break;
            
        case 6:
            $validated = [
                'already_in_use' => $data['already_in_use'] ?? '',
                'first_used_date' => sanitizeInput($data['first_used_date'] ?? '')
            ];
            break;
            
        case 7:
            $validated = [
                'similar_brands_aware' => $data['similar_brands_aware'] ?? '',
                'similar_brands_examples' => sanitizeInput($data['similar_brands_examples'] ?? '')
            ];
            break;
            
        case 9:
            $validated = [
                'legal_confirmations' => $data['legal_confirmations'] ?? []
            ];
            break;
    }
    
    return $validated;
}

function handleFileUploads(int $step, array $files): array {
    $uploaded = [];
    $allowedTypes = ['image/png', 'image/jpeg', 'image/svg+xml'];
    $maxSize = 5 * 1024 * 1024; // 5MB
    
    foreach ($files as $fieldName => $file) {
        if ($file['error'] === UPLOAD_ERR_OK) {
            if ($file['size'] > $maxSize) {
                continue; // Skip oversized files
            }
            
            if (!in_array($file['type'], $allowedTypes)) {
                continue; // Skip invalid file types
            }
            
            $filename = uniqid('trademark_', true) . '.' . pathinfo($file['name'], PATHINFO_EXTENSION);
            $uploadPath = __DIR__ . '/../uploads/trademarks/' . $filename;
            
            if (move_uploaded_file($file['tmp_name'], $uploadPath)) {
                $uploaded[$fieldName] = [
                    'filename' => $filename,
                    'original_name' => $file['name'],
                    'size' => $file['size'],
                    'type' => $file['type'],
                    'upload_date' => date('Y-m-d H:i:s')
                ];
            }
        }
    }
    
    return $uploaded;
}

function sanitizeInput(string $input): string {
    return htmlspecialchars(trim($input), ENT_QUOTES, 'UTF-8');
}

// Get step data
$stepData = $_SESSION['trademark_form']['data'][$currentStep] ?? [];
?>

<style>
        .trademark-container {
            max-width: 800px;
            margin: 0 auto;
            padding: 40px 20px;
        }
        
        .progress-bar {
            display: flex;
            justify-content: space-between;
            margin-bottom: 40px;
            position: relative;
        }
        
        .progress-bar::before {
            content: '';
            position: absolute;
            top: 20px;
            left: 0;
            right: 0;
            height: 2px;
            background: #e5e7eb;
            z-index: 1;
        }
        
        .progress-bar::after {
            content: '';
            position: absolute;
            top: 20px;
            left: 0;
            height: 2px;
            background: var(--primary);
            z-index: 2;
            width: <?=($currentStep - 1) * 11.11?>%;
            transition: width 0.3s ease;
        }
        
        .progress-step {
            position: relative;
            z-index: 3;
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 8px;
        }
        
        .progress-circle {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: white;
            border: 2px solid #e5e7eb;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            font-size: 14px;
            transition: all 0.3s ease;
        }
        
        .progress-step.completed .progress-circle {
            background: var(--primary);
            border-color: var(--primary);
            color: white;
        }
        
        .progress-step.active .progress-circle {
            border-color: var(--primary);
            color: var(--primary);
            box-shadow: 0 0 0 4px rgba(16, 185, 129, 0.1);
        }
        
        .progress-label {
            font-size: 12px;
            color: #64748b;
            text-align: center;
            max-width: 80px;
        }
        
        .step-card {
            background: white;
            border: 1px solid #e5e7eb;
            border-radius: 16px;
            padding: 40px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        
        .step-title {
            font-size: 24px;
            font-weight: 700;
            margin-bottom: 8px;
            color: #0f172a;
        }
        
        .step-subtitle {
            font-size: 16px;
            color: #64748b;
            margin-bottom: 32px;
        }
        
        .form-group {
            margin-bottom: 24px;
        }
        
        .form-label {
            display: block;
            font-weight: 600;
            margin-bottom: 8px;
            color: #374151;
        }
        
        .form-input {
            width: 100%;
            padding: 12px 16px;
            border: 1px solid #d1d5db;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.2s ease;
        }
        
        .form-input:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.1);
        }
        
        .form-textarea {
            width: 100%;
            min-height: 120px;
            padding: 12px 16px;
            border: 1px solid #d1d5db;
            border-radius: 8px;
            font-size: 16px;
            resize: vertical;
            transition: border-color 0.2s ease;
        }
        
        .form-textarea:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.1);
        }
        
        .radio-group, .checkbox-group {
            display: flex;
            flex-direction: column;
            gap: 12px;
        }
        
        .radio-option, .checkbox-option {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 16px;
            border: 1px solid #e5e7eb;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.2s ease;
        }
        
        .radio-option:hover, .checkbox-option:hover {
            border-color: var(--primary);
            background: rgba(16, 185, 129, 0.02);
        }
        
        .radio-option.selected, .checkbox-option.selected {
            border-color: var(--primary);
            background: rgba(16, 185, 129, 0.05);
        }
        
        .file-upload {
            border: 2px dashed #d1d5db;
            border-radius: 8px;
            padding: 40px;
            text-align: center;
            cursor: pointer;
            transition: all 0.2s ease;
        }
        
        .file-upload:hover {
            border-color: var(--primary);
            background: rgba(16, 185, 129, 0.02);
        }
        
        .file-upload.dragover {
            border-color: var(--primary);
            background: rgba(16, 185, 129, 0.05);
        }
        
        .button-group {
            display: flex;
            gap: 16px;
            justify-content: space-between;
            margin-top: 40px;
        }
        
        .btn {
            padding: 12px 24px;
            border-radius: 8px;
            font-weight: 600;
            font-size: 16px;
            border: none;
            cursor: pointer;
            transition: all 0.2s ease;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }
        
        .btn-primary {
            background: var(--primary);
            color: white;
        }
        
        .btn-primary:hover {
            background: #059669;
            transform: translateY(-1px);
        }
        
        .btn-secondary {
            background: white;
            color: #374151;
            border: 1px solid #d1d5db;
        }
        
        .btn-secondary:hover {
            background: #f9fafb;
            border-color: #9ca3af;
        }
        
        .protection-message {
            background: linear-gradient(135deg, #ecfdf5, #f0fdf4);
            border: 1px solid #bbf7d0;
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 32px;
        }
        
        .protection-message h3 {
            color: #065f46;
            margin-bottom: 8px;
            font-size: 18px;
        }
        
        .protection-message p {
            color: #047857;
            margin: 0;
            line-height: 1.6;
        }
        
        @media (max-width: 768px) {
            .trademark-container {
                padding: 20px 16px;
            }
            
            .progress-bar {
                flex-wrap: wrap;
                gap: 16px;
            }
            
            .progress-step {
                flex: 0 0 calc(33.333% - 16px);
                margin-bottom: 16px;
            }
            
            .progress-label {
                font-size: 10px;
            }
            
            .step-card {
                padding: 24px;
            }
            
            .button-group {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <div class="trademark-container">
        <!-- Progress Bar -->
        <div class="progress-bar">
            <?php for ($i = 1; $i <= 10; $i++): ?>
                <div class="progress-step <?= $i < $currentStep ? 'completed' : '' ?> <?= $i === $currentStep ? 'active' : '' ?>">
                    <div class="progress-circle">
                        <?= $i < $currentStep ? '✓' : $i ?>
                    </div>
                    <div class="progress-label">
                        <?= getStepLabel($i) ?>
                    </div>
                </div>
            <?php endfor; ?>
        </div>

        <!-- Protection Message -->
        <div class="protection-message">
            <h3>🛡️ Protect Your Brand Identity</h3>
            <p>Registering a trademark protects your brand from being copied or legally claimed by others. Secure your business name, logo, and brand elements today.</p>
        </div>

        <!-- Step Content -->
        <div class="step-card">
            <?php renderStepContent($currentStep, $stepData); ?>
        </div>
    </div>

    <script>
        // File upload handling
        document.addEventListener('DOMContentLoaded', function() {
            const fileUploads = document.querySelectorAll('.file-upload');
            
            fileUploads.forEach(upload => {
                const input = upload.querySelector('input[type="file"]');
                const label = upload.querySelector('label');
                
                if (input && label) {
                    label.addEventListener('click', () => input.click());
                    
                    input.addEventListener('change', function(e) {
                        const file = e.target.files[0];
                        if (file) {
                            label.innerHTML = `📎 ${file.name} (${formatFileSize(file.size)})`;
                            label.style.color = 'var(--primary)';
                        }
                    });
                    
                    // Drag and drop
                    upload.addEventListener('dragover', function(e) {
                        e.preventDefault();
                        upload.classList.add('dragover');
                    });
                    
                    upload.addEventListener('dragleave', function(e) {
                        e.preventDefault();
                        upload.classList.remove('dragover');
                    });
                    
                    upload.addEventListener('drop', function(e) {
                        e.preventDefault();
                        upload.classList.remove('dragover');
                        
                        const files = e.dataTransfer.files;
                        if (files.length > 0) {
                            input.files = files;
                            const event = new Event('change', { bubbles: true });
                            input.dispatchEvent(event);
                        }
                    });
                }
            });
            
            // Radio/Checkbox selection
            document.querySelectorAll('.radio-option, .checkbox-option').forEach(option => {
                option.addEventListener('click', function() {
                    const input = this.querySelector('input[type="radio"], input[type="checkbox"]');
                    if (input) {
                        if (input.type === 'radio') {
                            document.querySelectorAll('.radio-option').forEach(opt => opt.classList.remove('selected'));
                        }
                        input.checked = !input.checked;
                        this.classList.toggle('selected');
                    }
                });
            });
        });
        
        function formatFileSize(bytes) {
            if (bytes === 0) return '0 Bytes';
            const k = 1024;
            const sizes = ['Bytes', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));
            return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
        }
    </script>
</body>
</html>

<?php
function getStepLabel(int $step): string {
    $labels = [
        1 => 'Type',
        2 => 'Business',
        3 => 'Description',
        4 => 'Products',
        5 => 'Class',
        6 => 'Usage',
        7 => 'Similar',
        8 => 'Files',
        9 => 'Legal',
        10 => 'Summary'
    ];
    
    return $labels[$step] ?? '';
}

function renderStepContent(int $step, array $data): void {
    switch ($step) {
        case 1:
            renderStep1($data);
            break;
        case 2:
            renderStep2($data);
            break;
        case 3:
            renderStep3($data);
            break;
        case 4:
            renderStep4($data);
            break;
        case 5:
            renderStep5($data);
            break;
        case 6:
            renderStep6($data);
            break;
        case 7:
            renderStep7($data);
            break;
        case 8:
            renderStep8($data);
            break;
        case 9:
            renderStep9($data);
            break;
        case 10:
            renderStep10($data);
            break;
    }
}

function renderStep1(array $data): void {
    ?>
    <h1 class="step-title">Step 1 — Trademark Type</h1>
    <p class="step-subtitle">What type of trademark do you want to register?</p>
    
    <form method="POST" enctype="multipart/form-data">
        <div class="form-group">
            <label class="form-label">Trademark Type</label>
            <div class="radio-group">
                <div class="radio-option <?= ($data['trademark_type'] ?? '') === 'word' ? 'selected' : '' ?>">
                    <input type="radio" name="trademark_type" value="word" <?= ($data['trademark_type'] ?? '') === 'word' ? 'checked' : '' ?>>
                    <div>
                        <strong>Word Mark</strong>
                        <div style="color: #64748b; font-size: 14px;">Business name only</div>
                    </div>
                </div>
                <div class="radio-option <?= ($data['trademark_type'] ?? '') === 'logo' ? 'selected' : '' ?>">
                    <input type="radio" name="trademark_type" value="logo" <?= ($data['trademark_type'] ?? '') === 'logo' ? 'checked' : '' ?>>
                    <div>
                        <strong>Logo / Symbol</strong>
                        <div style="color: #64748b; font-size: 14px;">Visual design only</div>
                    </div>
                </div>
                <div class="radio-option <?= ($data['trademark_type'] ?? '') === 'combined' ? 'selected' : '' ?>">
                    <input type="radio" name="trademark_type" value="combined" <?= ($data['trademark_type'] ?? '') === 'combined' ? 'checked' : '' ?>>
                    <div>
                        <strong>Name + Logo</strong>
                        <div style="color: #64748b; font-size: 14px;">Combined mark</div>
                    </div>
                </div>
                <div class="radio-option <?= ($data['trademark_type'] ?? '') === 'slogan' ? 'selected' : '' ?>">
                    <input type="radio" name="trademark_type" value="slogan" <?= ($data['trademark_type'] ?? '') === 'slogan' ? 'checked' : '' ?>>
                    <div>
                        <strong>Slogan / Tagline</strong>
                        <div style="color: #64748b; font-size: 14px;">Marketing phrase</div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="form-group">
            <label class="form-label" for="trademark_name">Trademark Name</label>
            <input type="text" id="trademark_name" name="trademark_name" class="form-input" 
                   value="<?= htmlspecialchars($data['trademark_name'] ?? '') ?>" 
                   placeholder="Enter your trademark name" required>
        </div>
        
        <div class="form-group">
            <label class="form-label">Upload Logo (optional)</label>
            <div class="file-upload">
                <input type="file" name="logo" accept=".png,.jpg,.jpeg,.svg" style="display: none;">
                <label>
                    <div>📁 Click to upload or drag and drop</div>
                    <div style="color: #64748b; font-size: 14px; margin-top: 8px;">
                        Allowed formats: PNG / JPG / SVG<br>
                        Maximum size: 5MB
                    </div>
                </label>
            </div>
        </div>
        
        <div class="button-group">
            <button type="button" class="btn btn-secondary" onclick="window.history.back()">← Back</button>
            <button type="submit" name="next_step" class="btn btn-primary">Continue →</button>
        </div>
    </form>
    <?php
}

function renderStep2(array $data): void {
    ?>
    <h1 class="step-title">Step 2 — Business Details</h1>
    <p class="step-subtitle">Who owns this trademark?</p>
    
    <form method="POST">
        <div class="form-group">
            <label class="form-label" for="owner_name">Full Name / Company Name</label>
            <input type="text" id="owner_name" name="owner_name" class="form-input" 
                   value="<?= htmlspecialchars($data['owner_name'] ?? '') ?>" required>
        </div>
        
        <div class="form-group">
            <label class="form-label">Business Type</label>
            <div class="radio-group">
                <div class="radio-option <?= ($data['business_type'] ?? '') === 'individual' ? 'selected' : '' ?>">
                    <input type="radio" name="business_type" value="individual" <?= ($data['business_type'] ?? '') === 'individual' ? 'checked' : '' ?>>
                    <strong>Individual</strong>
                </div>
                <div class="radio-option <?= ($data['business_type'] ?? '') === 'business' ? 'selected' : '' ?>">
                    <input type="radio" name="business_type" value="business" <?= ($data['business_type'] ?? '') === 'business' ? 'checked' : '' ?>>
                    <strong>Business Name</strong>
                </div>
                <div class="radio-option <?= ($data['business_type'] ?? '') === 'limited' ? 'selected' : '' ?>">
                    <input type="radio" name="business_type" value="limited" <?= ($data['business_type'] ?? '') === 'limited' ? 'checked' : '' ?>>
                    <strong>Limited Company</strong>
                </div>
                <div class="radio-option <?= ($data['business_type'] ?? '') === 'ngo' ? 'selected' : '' ?>">
                    <input type="radio" name="business_type" value="ngo" <?= ($data['business_type'] ?? '') === 'ngo' ? 'checked' : '' ?>>
                    <strong>NGO</strong>
                </div>
            </div>
        </div>
        
        <div class="form-group">
            <label class="form-label" for="country">Country</label>
            <input type="text" id="country" name="country" class="form-input" 
                   value="<?= htmlspecialchars($data['country'] ?? '') ?>" required>
        </div>
        
        <div class="form-group">
            <label class="form-label" for="state">State</label>
            <input type="text" id="state" name="state" class="form-input" 
                   value="<?= htmlspecialchars($data['state'] ?? '') ?>" required>
        </div>
        
        <div class="form-group">
            <label class="form-label" for="city">City</label>
            <input type="text" id="city" name="city" class="form-input" 
                   value="<?= htmlspecialchars($data['city'] ?? '') ?>" required>
        </div>
        
        <div class="form-group">
            <label class="form-label" for="business_address">Business Address</label>
            <textarea id="business_address" name="business_address" class="form-textarea" required><?= htmlspecialchars($data['business_address'] ?? '') ?></textarea>
        </div>
        
        <div class="form-group">
            <label class="form-label" for="email">Email Address</label>
            <input type="email" id="email" name="email" class="form-input" 
                   value="<?= htmlspecialchars($data['email'] ?? '') ?>" required>
        </div>
        
        <div class="form-group">
            <label class="form-label" for="phone">Phone Number</label>
            <input type="tel" id="phone" name="phone" class="form-input" 
                   value="<?= htmlspecialchars($data['phone'] ?? '') ?>" required>
        </div>
        
        <div class="button-group">
            <button type="button" class="btn btn-secondary" onclick="window.location.href='?step=1'">← Back</button>
            <button type="submit" name="next_step" class="btn btn-primary">Continue →</button>
        </div>
    </form>
    <?php
}

function renderStep3(array $data): void {
    ?>
    <h1 class="step-title">Step 3 — Describe Your Business</h1>
    <p class="step-subtitle">This helps determine the correct trademark class.</p>
    
    <form method="POST">
        <div class="form-group">
            <label class="form-label" for="business_description">Business Description</label>
            <textarea id="business_description" name="business_description" class="form-textarea" 
                      placeholder="Describe what your business sells or services you provide." required><?= htmlspecialchars($data['business_description'] ?? '') ?></textarea>
            <div style="margin-top: 8px; color: #64748b; font-size: 14px;">
                <strong>Example answers:</strong>
                <ul style="margin: 8px 0; padding-left: 20px;">
                    <li>Clothing brand</li>
                    <li>Digital marketing agency</li>
                    <li>Restaurant</li>
                    <li>Software development company</li>
                </ul>
            </div>
        </div>
        
        <div class="button-group">
            <button type="button" class="btn btn-secondary" onclick="window.location.href='?step=2'">← Back</button>
            <button type="submit" name="next_step" class="btn btn-primary">Continue →</button>
        </div>
    </form>
    <?php
}

function renderStep4(array $data): void {
    ?>
    <h1 class="step-title">Step 4 — Products or Services</h1>
    <p class="step-subtitle">What exactly does your business sell?</p>
    
    <form method="POST">
        <div class="form-group">
            <label class="form-label">Select Categories</label>
            <div class="checkbox-group">
                <?php
                $categories = [
                    'technology' => 'Technology',
                    'fashion' => 'Fashion / Clothing',
                    'food' => 'Food / Restaurant',
                    'consulting' => 'Consulting',
                    'education' => 'Education',
                    'real_estate' => 'Real Estate',
                    'beauty' => 'Beauty / Cosmetics',
                    'retail' => 'Retail / Ecommerce',
                    'media' => 'Media / Entertainment',
                    'other' => 'Other'
                ];
                
                $selectedCategories = $data['product_categories'] ?? [];
                
                foreach ($categories as $value => $label):
                ?>
                <div class="checkbox-option <?= in_array($value, $selectedCategories) ? 'selected' : '' ?>">
                    <input type="checkbox" name="product_categories[]" value="<?= $value ?>" 
                           <?= in_array($value, $selectedCategories) ? 'checked' : '' ?>>
                    <strong><?= $label ?></strong>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
        
        <div class="form-group">
            <label class="form-label" for="products_services_description">Describe Products or Services</label>
            <textarea id="products_services_description" name="products_services_description" class="form-textarea" 
                      placeholder="Please provide more details about your specific products or services..." required><?= htmlspecialchars($data['products_services_description'] ?? '') ?></textarea>
        </div>
        
        <div class="button-group">
            <button type="button" class="btn btn-secondary" onclick="window.location.href='?step=3'">← Back</button>
            <button type="submit" name="next_step" class="btn btn-primary">Continue →</button>
        </div>
    </form>
    <?php
}

function renderStep5(array $data): void {
    ?>
    <h1 class="step-title">Step 5 — Trademark Class (Optional)</h1>
    <p class="step-subtitle">Advanced users may know their class. If empty, Go Legal determines class.</p>
    
    <form method="POST">
        <div class="form-group">
            <label class="form-label" for="preferred_class">Preferred Trademark Class (optional)</label>
            <input type="text" id="preferred_class" name="preferred_class" class="form-input" 
                   value="<?= htmlspecialchars($data['preferred_class'] ?? '') ?>" 
                   placeholder="Example: Class 25 (Clothing)">
            <div style="margin-top: 8px; color: #64748b; font-size: 14px;">
                Leave empty if you're unsure - our team will determine the appropriate class for your trademark.
            </div>
        </div>
        
        <div class="button-group">
            <button type="button" class="btn btn-secondary" onclick="window.location.href='?step=4'">← Back</button>
            <button type="submit" name="next_step" class="btn btn-primary">Continue →</button>
        </div>
    </form>
    <?php
}

function renderStep6(array $data): void {
    ?>
    <h1 class="step-title">Step 6 — Trademark Use Status</h1>
    <p class="step-subtitle">Is your trademark already in use?</p>
    
    <form method="POST">
        <div class="form-group">
            <label class="form-label">Current Usage Status</label>
            <div class="radio-group">
                <div class="radio-option <?= ($data['already_in_use'] ?? '') === 'yes' ? 'selected' : '' ?>">
                    <input type="radio" name="already_in_use" value="yes" <?= ($data['already_in_use'] ?? '') === 'yes' ? 'checked' : '' ?>>
                    <div>
                        <strong>Yes — already using it</strong>
                    </div>
                </div>
                <div class="radio-option <?= ($data['already_in_use'] ?? '') === 'no' ? 'selected' : '' ?>">
                    <input type="radio" name="already_in_use" value="no" <?= ($data['already_in_use'] ?? '') === 'no' ? 'checked' : '' ?>>
                    <div>
                        <strong>No — planning to launch soon</strong>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="form-group">
            <label class="form-label" for="first_used_date">Date First Used (optional)</label>
            <input type="date" id="first_used_date" name="first_used_date" class="form-input" 
                   value="<?= htmlspecialchars($data['first_used_date'] ?? '') ?>">
        </div>
        
        <div class="button-group">
            <button type="button" class="btn btn-secondary" onclick="window.location.href='?step=5'">← Back</button>
            <button type="submit" name="next_step" class="btn btn-primary">Continue →</button>
        </div>
    </form>
    <?php
}

function renderStep7(array $data): void {
    ?>
    <h1 class="step-title">Step 7 — Similar Brand Check</h1>
    <p class="step-subtitle">Are you aware of any similar brands using this name?</p>
    
    <form method="POST">
        <div class="form-group">
            <label class="form-label">Similar Brands Awareness</label>
            <div class="radio-group">
                <div class="radio-option <?= ($data['similar_brands_aware'] ?? '') === 'yes' ? 'selected' : '' ?>">
                    <input type="radio" name="similar_brands_aware" value="yes" <?= ($data['similar_brands_aware'] ?? '') === 'yes' ? 'checked' : '' ?>>
                    <div>
                        <strong>Yes</strong>
                    </div>
                </div>
                <div class="radio-option <?= ($data['similar_brands_aware'] ?? '') === 'no' ? 'selected' : '' ?>">
                    <input type="radio" name="similar_brands_aware" value="no" <?= ($data['similar_brands_aware'] ?? '') === 'no' ? 'checked' : '' ?>>
                    <div>
                        <strong>No</strong>
                    </div>
                </div>
                <div class="radio-option <?= ($data['similar_brands_aware'] ?? '') === 'not_sure' ? 'selected' : '' ?>">
                    <input type="radio" name="similar_brands_aware" value="not_sure" <?= ($data['similar_brands_aware'] ?? '') === 'not_sure' ? 'checked' : '' ?>>
                    <div>
                        <strong>Not sure</strong>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="form-group">
            <label class="form-label" for="similar_brands_examples">Provide Examples (if any)</label>
            <textarea id="similar_brands_examples" name="similar_brands_examples" class="form-textarea" 
                      placeholder="List any similar brands you're aware of..."><?= htmlspecialchars($data['similar_brands_examples'] ?? '') ?></textarea>
        </div>
        
        <div class="button-group">
            <button type="button" class="btn btn-secondary" onclick="window.location.href='?step=6'">← Back</button>
            <button type="submit" name="next_step" class="btn btn-primary">Continue →</button>
        </div>
    </form>
    <?php
}

function renderStep8(array $data): void {
    ?>
    <h1 class="step-title">Step 8 — Upload Supporting Files</h1>
    <p class="step-subtitle">This helps strengthen your filing.</p>
    
    <form method="POST" enctype="multipart/form-data">
        <div class="form-group">
            <label class="form-label">Logo File</label>
            <div class="file-upload">
                <input type="file" name="logo_file" accept=".png,.jpg,.jpeg,.svg" style="display: none;">
                <label>
                    <div>📁 Click to upload logo</div>
                    <div style="color: #64748b; font-size: 14px; margin-top: 8px;">
                        PNG / JPG / SVG • Max 5MB
                    </div>
                </label>
            </div>
        </div>
        
        <div class="form-group">
            <label class="form-label">Brand Mockup (optional)</label>
            <div class="file-upload">
                <input type="file" name="brand_mockup" accept=".png,.jpg,.jpeg,.svg,.pdf" style="display: none;">
                <label>
                    <div>📁 Click to upload brand mockup</div>
                    <div style="color: #64748b; font-size: 14px; margin-top: 8px;">
                        PNG / JPG / SVG / PDF • Max 5MB
                    </div>
                </label>
            </div>
        </div>
        
        <div class="form-group">
            <label class="form-label">Product Image (optional)</label>
            <div class="file-upload">
                <input type="file" name="product_image" accept=".png,.jpg,.jpeg,.svg" style="display: none;">
                <label>
                    <div>📁 Click to upload product image</div>
                    <div style="color: #64748b; font-size: 14px; margin-top: 8px;">
                        PNG / JPG / SVG • Max 5MB
                    </div>
                </label>
            </div>
        </div>
        
        <div class="button-group">
            <button type="button" class="btn btn-secondary" onclick="window.location.href='?step=7'">← Back</button>
            <button type="submit" name="next_step" class="btn btn-primary">Continue →</button>
        </div>
    </form>
    <?php
}

function renderStep9(array $data): void {
    ?>
    <h1 class="step-title">Step 9 — Confirmation & Legal Authorization</h1>
    <p class="step-subtitle">Please review and confirm the following statements:</p>
    
    <form method="POST">
        <div class="form-group">
            <div class="checkbox-group">
                <div class="checkbox-option">
                    <input type="checkbox" name="legal_confirmations[]" value="ownership" 
                           <?= in_array('ownership', $data['legal_confirmations'] ?? []) ? 'checked' : '' ?> required>
                    <div>
                        <strong>I confirm this trademark belongs to me or my business</strong>
                    </div>
                </div>
                <div class="checkbox-option">
                    <input type="checkbox" name="legal_confirmations[]" value="authorization" 
                           <?= in_array('authorization', $data['legal_confirmations'] ?? []) ? 'checked' : '' ?> required>
                    <div>
                        <strong>I authorize Go Legal to file this trademark application on my behalf</strong>
                    </div>
                </div>
                <div class="checkbox-option">
                    <input type="checkbox" name="legal_confirmations[]" value="understanding" 
                           <?= in_array('understanding', $data['legal_confirmations'] ?? []) ? 'checked' : '' ?> required>
                    <div>
                        <strong>I understand approval depends on trademark registry examination</strong>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="button-group">
            <button type="button" class="btn btn-secondary" onclick="window.location.href='?step=8'">← Back</button>
            <button type="submit" name="next_step" class="btn btn-primary">Continue →</button>
        </div>
    </form>
    <?php
}

function renderStep10(array $data): void {
    ?>
    <h1 class="step-title">Step 10 — Order Summary</h1>
    <p class="step-subtitle">Review your trademark application details</p>
    
    <div style="background: #f8fafc; border: 1px solid #e5e7eb; border-radius: 12px; padding: 24px; margin-bottom: 32px;">
        <h3 style="margin-bottom: 16px; color: #1f2937;">Application Details</h3>
        
        <?php
        $formData = $_SESSION['trademark_form']['data'] ?? [];
        
        echo '<div style="display: grid; gap: 12px;">';
        
        if (!empty($formData[1]['trademark_name'])) {
            echo '<div><strong>Trademark Name:</strong> ' . htmlspecialchars($formData[1]['trademark_name']) . '</div>';
        }
        
        if (!empty($formData[1]['trademark_type'])) {
            $types = [
                'word' => 'Word Mark',
                'logo' => 'Logo / Symbol',
                'combined' => 'Name + Logo',
                'slogan' => 'Slogan / Tagline'
            ];
            echo '<div><strong>Type:</strong> ' . ($types[$formData[1]['trademark_type']] ?? $formData[1]['trademark_type']) . '</div>';
        }
        
        if (!empty($formData[2]['owner_name'])) {
            echo '<div><strong>Owner:</strong> ' . htmlspecialchars($formData[2]['owner_name']) . '</div>';
        }
        
        if (!empty($formData[2]['business_type'])) {
            $businessTypes = [
                'individual' => 'Individual',
                'business' => 'Business Name',
                'limited' => 'Limited Company',
                'ngo' => 'NGO'
            ];
            echo '<div><strong>Business Type:</strong> ' . ($businessTypes[$formData[2]['business_type']] ?? $formData[2]['business_type']) . '</div>';
        }
        
        echo '</div>';
        ?>
    </div>
    
    <div style="background: linear-gradient(135deg, #f0f9ff, #e0f2fe); border: 1px solid #bae6fd; border-radius: 12px; padding: 24px; margin-bottom: 32px;">
        <h3 style="margin-bottom: 16px; color: #0c4a6e;">Services Included</h3>
        
        <div style="display: grid; gap: 12px;">
            <div>✅ Trademark Search</div>
            <div>✅ Trademark Filing</div>
            <div>✅ Publication Monitoring</div>
            <div>✅ Certificate Processing</div>
        </div>
    </div>
    
    <div style="background: linear-gradient(135deg, #fef3c7, #fde68a); border: 1px solid #fcd34d; border-radius: 12px; padding: 24px; margin-bottom: 32px;">
        <h3 style="margin-bottom: 16px; color: #78350f;">Pricing</h3>
        
        <div style="display: grid; gap: 8px; margin-bottom: 16px;">
            <div style="display: flex; justify-content: space-between;">
                <span>Trademark Search:</span>
                <strong>₦25,000</strong>
            </div>
            <div style="display: flex; justify-content: space-between;">
                <span>Trademark Filing:</span>
                <strong>₦50,000</strong>
            </div>
            <div style="display: flex; justify-content: space-between;">
                <span>Publication Monitoring:</span>
                <strong>₦15,000</strong>
            </div>
            <div style="display: flex; justify-content: space-between;">
                <span>Certificate Processing:</span>
                <strong>₦10,000</strong>
            </div>
        </div>
        
        <div style="border-top: 2px solid #f59e0b; padding-top: 16px; display: flex; justify-content: space-between; align-items: center;">
            <span style="font-size: 18px; font-weight: 700;">Total:</span>
            <strong style="font-size: 24px; color: #d97706;">₦100,000</strong>
        </div>
    </div>
    
    <form method="POST">
        <div class="button-group">
            <button type="button" class="btn btn-secondary" onclick="window.location.href='?step=9'">← Back</button>
            <button type="submit" name="save_later" class="btn btn-secondary">Save Application</button>
            <button type="submit" name="proceed_to_payment" class="btn btn-primary">Proceed to Payment</button>
        </div>
    </form>
    <?php
}

require_once __DIR__ . '/../includes/footer.php';
?>
