<?php

declare(strict_types=1);

/**
 * Super Admin Routes
 *
 * All routes for the Super Admin panel.
 *
 * URL prefix: /super-admin/*
 */

use App\Core\Router;

/** @var Router $router */

// ─── Super Admin Authentication ──────────────────────────────────────────────

$router->get('/super-admin/login', 'SuperAdmin\Controllers\SuperAdminAuthController@loginForm');
$router->post('/super-admin/login', 'SuperAdmin\Controllers\SuperAdminAuthController@login');
$router->post('/super-admin/logout', 'SuperAdmin\Controllers\SuperAdminAuthController@logout');

// ─── Super Admin Dashboard ───────────────────────────────────────────────────

$router->get('/super-admin', 'SuperAdmin\Controllers\SuperAdminDashboardController@index', ['super_admin']);
$router->get('/super-admin/dashboard', 'SuperAdmin\Controllers\SuperAdminDashboardController@index', ['super_admin']);

// ─── Branches ────────────────────────────────────────────────────────────────

$router->get('/super-admin/branches', 'SuperAdmin\Controllers\SuperAdminBranchController@index', ['super_admin']);
$router->get('/super-admin/branches/create', 'SuperAdmin\Controllers\SuperAdminBranchController@create', ['super_admin']);
$router->post('/super-admin/branches', 'SuperAdmin\Controllers\SuperAdminBranchController@store', ['super_admin']);
$router->get('/super-admin/branches/{id}/edit', 'SuperAdmin\Controllers\SuperAdminBranchController@edit', ['super_admin']);
$router->post('/super-admin/branches/{id}', 'SuperAdmin\Controllers\SuperAdminBranchController@update', ['super_admin']);
$router->post('/super-admin/branches/{id}/delete', 'SuperAdmin\Controllers\SuperAdminBranchController@delete', ['super_admin']);
$router->post('/super-admin/branches/{id}/suspend', 'SuperAdmin\Controllers\SuperAdminBranchController@delete', ['super_admin']);
$router->post('/super-admin/branches/{id}/hard-delete', 'SuperAdmin\Controllers\SuperAdminBranchController@hardDelete', ['super_admin']);
$router->get('/super-admin/branches/{id}', 'SuperAdmin\Controllers\SuperAdminBranchController@show', ['super_admin']);

// ─── Branch Admins ───────────────────────────────────────────────────────────

$router->get('/super-admin/branch-admins', 'SuperAdmin\Controllers\SuperAdminBranchAdminController@index', ['super_admin']);
$router->get('/super-admin/branch-admins/create', 'SuperAdmin\Controllers\SuperAdminBranchAdminController@create', ['super_admin']);
$router->post('/super-admin/branch-admins', 'SuperAdmin\Controllers\SuperAdminBranchAdminController@store', ['super_admin']);
$router->get('/super-admin/branch-admins/{id}/edit', 'SuperAdmin\Controllers\SuperAdminBranchAdminController@edit', ['super_admin']);
$router->post('/super-admin/branch-admins/{id}', 'SuperAdmin\Controllers\SuperAdminBranchAdminController@update', ['super_admin']);
$router->post('/super-admin/branch-admins/{id}/delete', 'SuperAdmin\Controllers\SuperAdminBranchAdminController@delete', ['super_admin']);

// ─── Branch Applications ─────────────────────────────────────────────────────

$router->get('/super-admin/branch-applications', 'SuperAdmin\Controllers\SuperAdminBranchApplicationController@index', ['super_admin']);
$router->get('/super-admin/branch-applications/{id}', 'SuperAdmin\Controllers\SuperAdminBranchApplicationController@show', ['super_admin']);
$router->post('/super-admin/branch-applications/{id}/approve', 'SuperAdmin\Controllers\SuperAdminBranchApplicationController@approve', ['super_admin']);
$router->post('/super-admin/branch-applications/{id}/reject', 'SuperAdmin\Controllers\SuperAdminBranchApplicationController@reject', ['super_admin']);
$router->post('/super-admin/branch-applications/{id}/delete', 'SuperAdmin\Controllers\SuperAdminBranchApplicationController@delete', ['super_admin']);

// ─── Certificate Templates (OLD) ─────────────────────────────────────────────

$router->get('/super-admin/certificate-templates', 'SuperAdmin\Controllers\SuperAdminCertificateTemplateController@index', ['super_admin']);
$router->get('/super-admin/certificate-templates/create', 'SuperAdmin\Controllers\SuperAdminCertificateTemplateController@create', ['super_admin']);
$router->post('/super-admin/certificate-templates', 'SuperAdmin\Controllers\SuperAdminCertificateTemplateController@store', ['super_admin']);
$router->get('/super-admin/certificate-templates/{id}', 'SuperAdmin\Controllers\SuperAdminCertificateTemplateController@show', ['super_admin']);
$router->get('/super-admin/certificate-templates/{id}/edit', 'SuperAdmin\Controllers\SuperAdminCertificateTemplateController@edit', ['super_admin']);
$router->post('/super-admin/certificate-templates/{id}', 'SuperAdmin\Controllers\SuperAdminCertificateTemplateController@update', ['super_admin']);
$router->post('/super-admin/certificate-templates/{id}/delete', 'SuperAdmin\Controllers\SuperAdminCertificateTemplateController@delete', ['super_admin']);
$router->post('/super-admin/certificate-templates/{id}/fields', 'SuperAdmin\Controllers\SuperAdminCertificateTemplateController@addField', ['super_admin']);
$router->post('/super-admin/certificate-templates/{id}/fields/{field_id}', 'SuperAdmin\Controllers\SuperAdminCertificateTemplateController@updateField', ['super_admin']);
$router->post('/super-admin/certificate-templates/{id}/fields/{field_id}/delete', 'SuperAdmin\Controllers\SuperAdminCertificateTemplateController@deleteField', ['super_admin']);

// ─── Course Subjects ─────────────────────────────────────────────────────────

$router->get('/super-admin/course-subjects', 'SuperAdmin\Controllers\SuperAdminCourseSubjectController@index', ['super_admin']);
$router->get('/super-admin/course-subjects/{courseId}/edit', 'SuperAdmin\Controllers\SuperAdminCourseSubjectController@edit', ['super_admin']);
$router->post('/super-admin/course-subjects/save', 'SuperAdmin\Controllers\SuperAdminCourseSubjectController@save', ['super_admin']);

// ─── OLD Cert & Marksheet Mapping (Legacy) ──────────────────────────────────

$router->get('/super-admin/cert-marksheet-mapping', 'SuperAdmin\Controllers\SuperAdminCertMarksheetMappingController@index', ['super_admin']);
$router->get('/super-admin/cert-marksheet-mapping/{courseId}/{type}', 'SuperAdmin\Controllers\SuperAdminCertMarksheetMappingController@edit', ['super_admin']);
$router->post('/super-admin/cert-marksheet-mapping/save', 'SuperAdmin\Controllers\SuperAdminCertMarksheetMappingController@save', ['super_admin']);
$router->get('/super-admin/cert-marksheet-mapping/preview/{templateId}', 'SuperAdmin\Controllers\SuperAdminCertMarksheetMappingController@preview', ['super_admin']);

// ═══════════════════════════════════════════════════════════════════════════════
// ─── Phase 5: CERTIFICATE PATTERNS — NEW REUSABLE LAYOUT SYSTEM ─────────────
// ═══════════════════════════════════════════════════════════════════════════════

// Pattern CRUD
$router->get('/super-admin/patterns', 'SuperAdmin\Controllers\SuperAdminPatternController@index', ['super_admin']);
$router->get('/super-admin/patterns/create', 'SuperAdmin\Controllers\SuperAdminPatternController@create', ['super_admin']);
$router->post('/super-admin/patterns/create', 'SuperAdmin\Controllers\SuperAdminPatternController@create', ['super_admin']);
$router->get('/super-admin/patterns/edit/{id}', 'SuperAdmin\Controllers\SuperAdminPatternController@edit', ['super_admin']);
$router->post('/super-admin/patterns/edit/{id}', 'SuperAdmin\Controllers\SuperAdminPatternController@edit', ['super_admin']);

// Background upload (AJAX)
$router->post('/super-admin/patterns/upload-background/{id}', 'SuperAdmin\Controllers\SuperAdminPatternController@uploadBackground', ['super_admin']);

// Save mapping (AJAX)
$router->post('/super-admin/patterns/save-ajax/{id}', 'SuperAdmin\Controllers\SuperAdminPatternController@saveAjax', ['super_admin']);

// Live Preview
$router->get('/super-admin/patterns/preview/{id}', 'SuperAdmin\Controllers\SuperAdminPatternController@preview', ['super_admin']);
$router->post('/super-admin/patterns/preview/{id}', 'SuperAdmin\Controllers\SuperAdminPatternController@preview', ['super_admin']);

// Lock / Unlock / Clone / Default / Delete
$router->get('/super-admin/patterns/lock/{id}', 'SuperAdmin\Controllers\SuperAdminPatternController@lock', ['super_admin']);
$router->get('/super-admin/patterns/unlock/{id}', 'SuperAdmin\Controllers\SuperAdminPatternController@unlock', ['super_admin']);
$router->get('/super-admin/patterns/clone/{id}', 'SuperAdmin\Controllers\SuperAdminPatternController@clone', ['super_admin']);
$router->get('/super-admin/patterns/set-default/{id}', 'SuperAdmin\Controllers\SuperAdminPatternController@setDefault', ['super_admin']);
$router->get('/super-admin/patterns/delete/{id}', 'SuperAdmin\Controllers\SuperAdminPatternController@delete', ['super_admin']);

// Assign pattern to a course (AJAX from course form)
$router->post('/super-admin/patterns/assign-course', 'SuperAdmin\Controllers\SuperAdminPatternController@assignToCourse', ['super_admin']);

// Test photo upload for preview (AJAX)
$router->post('/super-admin/patterns/upload-test-photo', 'SuperAdmin\Controllers\SuperAdminPatternController@uploadTestPhoto', ['super_admin']);
$router->get('/super-admin/patterns/clear-test-photo', 'SuperAdmin\Controllers\SuperAdminPatternController@clearTestPhoto', ['super_admin']);

// ─── Pattern Course Assignment (NEW) ─────────────────────────────────────────
$router->get('/super-admin/patterns/course-assignment', 'SuperAdmin\Controllers\PatternCourseController@index', ['super_admin']);
$router->post('/super-admin/patterns/course-assignment/save', 'SuperAdmin\Controllers\PatternCourseController@save', ['super_admin']);
$router->get('/super-admin/patterns/test-generate', 'SuperAdmin\Controllers\PatternCourseController@testGenerate', ['super_admin']);

// ═══════════════════════════════════════════════════════════════════════════════
// ─── SUPER ADMIN RESULT CREATION (Auto-Approve + Auto-Generate PDFs) ─────────
// Single-page form: student details + result entry + payment in ONE form.
// On submit → status='generated' (auto-approved) + both PDFs generated.
// Institute: GMIND INSTITUTE SULTANPUR | Branch Code: GMBR01
// ═══════════════════════════════════════════════════════════════════════════════

$router->get('/super-admin/cert-gen', 'SuperAdmin\Controllers\SuperAdminCertGenController@index', ['super_admin']);
$router->get('/super-admin/cert-gen/new', 'SuperAdmin\Controllers\SuperAdminCertGenController@create', ['super_admin']);
$router->post('/super-admin/cert-gen/store', 'SuperAdmin\Controllers\SuperAdminCertGenController@store', ['super_admin']);
$router->get('/super-admin/cert-gen/download/{id}', 'SuperAdmin\Controllers\SuperAdminCertGenController@download', ['super_admin']);
$router->get('/super-admin/cert-gen/regenerate/{id}', 'SuperAdmin\Controllers\SuperAdminCertGenController@regenerate', ['super_admin']);

// ─── Document Approvals ─────────────────────────────────────────────────────

$router->get('/super-admin/documents', 'SuperAdmin\Controllers\SuperAdminDocumentController@index', ['super_admin']);
$router->get('/super-admin/documents/{id}', 'SuperAdmin\Controllers\SuperAdminDocumentController@show', ['super_admin']);
$router->post('/super-admin/documents/{id}/approve', 'SuperAdmin\Controllers\SuperAdminDocumentController@approve', ['super_admin']);
$router->post('/super-admin/documents/{id}/reject', 'SuperAdmin\Controllers\SuperAdminDocumentController@reject', ['super_admin']);

// ─── Announcements ───────────────────────────────────────────────────────────

$router->get('/super-admin/announcements', 'SuperAdmin\Controllers\SuperAdminAnnouncementController@index', ['super_admin']);
$router->get('/super-admin/announcements/create', 'SuperAdmin\Controllers\SuperAdminAnnouncementController@create', ['super_admin']);
$router->post('/super-admin/announcements', 'SuperAdmin\Controllers\SuperAdminAnnouncementController@store', ['super_admin']);
$router->get('/super-admin/announcements/{id}/edit', 'SuperAdmin\Controllers\SuperAdminAnnouncementController@edit', ['super_admin']);
$router->post('/super-admin/announcements/{id}', 'SuperAdmin\Controllers\SuperAdminAnnouncementController@update', ['super_admin']);
$router->post('/super-admin/announcements/{id}/delete', 'SuperAdmin\Controllers\SuperAdminAnnouncementController@delete', ['super_admin']);

// ─── System Settings ─────────────────────────────────────────────────────────

$router->get('/super-admin/settings', 'SuperAdmin\Controllers\SuperAdminSettingsController@index', ['super_admin']);
$router->post('/super-admin/settings', 'SuperAdmin\Controllers\SuperAdminSettingsController@update', ['super_admin']);

// ═══════════════════════════════════════════════════════════════════════════════
// ─── BRANCH BATCH ROUTES (Fallback Registration) ─────────────────────────────
// These routes are registered HERE (in super_admin.php) as a fallback guarantee
// because this file is definitely loaded by routes/web.php. If the same routes
// are also registered in routes/branch.php, the duplicates are harmless (the
// Router stores routes in an associative array keyed by METHOD:PATH, so the
// last registration wins — no errors).
//
// This ensures the Edit button on the batches list page never 404s.
// ═══════════════════════════════════════════════════════════════════════════════

$router->get('/branch/batches', 'BranchAdmin\Controllers\BranchBatchController@index', ['branch_admin']);
$router->get('/branch/batches/create', 'BranchAdmin\Controllers\BranchBatchController@create', ['branch_admin']);
$router->post('/branch/batches', 'BranchAdmin\Controllers\BranchBatchController@store', ['branch_admin']);
$router->get('/branch/batches/{id}/edit', 'BranchAdmin\Controllers\BranchBatchController@edit', ['branch_admin']);
$router->post('/branch/batches/{id}/update', 'BranchAdmin\Controllers\BranchBatchController@update', ['branch_admin']);
$router->post('/branch/batches/{id}/delete', 'BranchAdmin\Controllers\BranchBatchController@delete', ['branch_admin']);

// ═══════════════════════════════════════════════════════════════════════════════
// ─── BRANCH STUDENT ROUTES (Fallback Registration) ───────────────────────────
// ═══════════════════════════════════════════════════════════════════════════════

$router->get('/branch/students', 'BranchAdmin\Controllers\BranchStudentController@index', ['branch_admin']);
$router->get('/branch/students/create', 'BranchAdmin\Controllers\BranchStudentController@create', ['branch_admin']);
$router->post('/branch/students', 'BranchAdmin\Controllers\BranchStudentController@store', ['branch_admin']);
$router->get('/branch/students/{id}', 'BranchAdmin\Controllers\BranchStudentController@show', ['branch_admin']);
$router->get('/branch/students/{id}/edit', 'BranchAdmin\Controllers\BranchStudentController@edit', ['branch_admin']);
$router->post('/branch/students/{id}/update', 'BranchAdmin\Controllers\BranchStudentController@update', ['branch_admin']);
$router->post('/branch/students/{id}/delete', 'BranchAdmin\Controllers\BranchStudentController@delete', ['branch_admin']);
$router->get('/branch/students/{id}/admission-preview', 'BranchAdmin\Controllers\BranchStudentController@admissionPreview', ['branch_admin']);
$router->get('/branch/students/{id}/admission-pdf', 'BranchAdmin\Controllers\BranchStudentController@admissionPdf', ['branch_admin']);

// ═══════════════════════════════════════════════════════════════════════════════
// ─── BRANCH FEE ROUTES (Fallback Registration) ───────────────────────────────
// ═══════════════════════════════════════════════════════════════════════════════

$router->get('/branch/fees', 'BranchAdmin\Controllers\BranchFeeController@index', ['branch_admin']);
$router->get('/branch/fees/collect/{id}', 'BranchAdmin\Controllers\BranchFeeController@collect', ['branch_admin']);
$router->post('/branch/fees/collect/{id}/store', 'BranchAdmin\Controllers\BranchFeeController@store', ['branch_admin']);
$router->get('/branch/fees/receipt-preview/{id}', 'BranchAdmin\Controllers\BranchFeeController@receiptPreview', ['branch_admin']);
$router->get('/branch/fees/receipt/{id}', 'BranchAdmin\Controllers\BranchFeeController@receipt', ['branch_admin']);
$router->get('/branch/fees/reports', 'BranchAdmin\Controllers\BranchFeeController@reports', ['branch_admin']);
$router->get('/branch/fees/export-pdf', 'BranchAdmin\Controllers\BranchFeeController@exportPdf', ['branch_admin']);

// ═══════════════════════════════════════════════════════════════════════════════
// ─── PUBLIC BRANCH REGISTRATION (Franchise Application) ──────────────────────
// Accessible without login at /branch/register — front of website
// ═══════════════════════════════════════════════════════════════════════════════

$router->get('/branch/register', 'BranchAdmin\Controllers\BranchApplicationController@form');
$router->post('/branch/register', 'BranchAdmin\Controllers\BranchApplicationController@submit');

// ═══════════════════════════════════════════════════════════════════════════════
// ─── BRANCH PRINT QUEUE (Bulk Certificate Printing) ──────────────────────────
// ═══════════════════════════════════════════════════════════════════════════════

$router->get('/branch/print-queue', 'BranchAdmin\Controllers\BranchPrintQueueController@index', ['branch_admin']);
$router->post('/branch/print-queue/bulk-download', 'BranchAdmin\Controllers\BranchPrintQueueController@bulkDownload', ['branch_admin']);

// ═══════════════════════════════════════════════════════════════════════════════
// ─── BRANCH COURSES (My Courses & Fees) + WALLET + FACILITIES ────────────────
// ═══════════════════════════════════════════════════════════════════════════════

$router->get('/branch/courses', 'BranchAdmin\Controllers\BranchCourseController@index', ['branch_admin']);
$router->post('/branch/courses/save', 'BranchAdmin\Controllers\BranchCourseController@save', ['branch_admin']);

$router->get('/branch/wallet', 'BranchAdmin\Controllers\BranchWalletController@index', ['branch_admin']);
$router->get('/branch/wallet/history', 'BranchAdmin\Controllers\BranchWalletController@history', ['branch_admin']);

$router->get('/branch/facilities', 'BranchAdmin\Controllers\BranchFacilityController@index', ['branch_admin']);
$router->post('/branch/facilities', 'BranchAdmin\Controllers\BranchFacilityController@store', ['branch_admin']);
$router->post('/branch/facilities/{id}/delete', 'BranchAdmin\Controllers\BranchFacilityController@delete', ['branch_admin']);

// ═══════════════════════════════════════════════════════════════════════════════
// ─── BRANCH ID CARDS (Modern Design + Bulk Print on A4) ──────────────────────
// ═══════════════════════════════════════════════════════════════════════════════

$router->get('/branch/id-cards', 'BranchAdmin\Controllers\BranchIdCardController@index', ['branch_admin']);
$router->get('/branch/id-cards/generate/{id}', 'BranchAdmin\Controllers\BranchIdCardController@generate', ['branch_admin']);
$router->post('/branch/id-cards/bulk-print', 'BranchAdmin\Controllers\BranchIdCardController@bulkPrint', ['branch_admin']);
