<?php

/**
 * Organization Configuration Module Routes
 *
 * These routes handle all functionality for the Organization Configuration Module
 * including organization details, leadership structure, election cycles, voting procedures,
 * leadership history, staff history, and organogram visualization.
 *
 * All routes use controller-based routing (required for route caching).
 * Routes are protected by auth and verified middleware.
 * Access is restricted to Super Manager and Manager roles via permission checks in controllers.
 */

use App\Http\Controllers\OrganizationController;
use App\Http\Controllers\LeadershipPositionController;
use App\Http\Controllers\ElectionCycleController;
use App\Http\Controllers\VotingProcedureController;
use App\Http\Controllers\LeadershipHistoryController;
use App\Http\Controllers\StaffHistoryController;
use App\Http\Controllers\OrganogramController;
use Illuminate\Support\Facades\Route;

// Organization Configuration Module Routes
Route::middleware(['auth', 'verified'])
    ->prefix('organization')
    ->name('organization.')
    ->group(function () {

        // Module landing page
        Route::get('/', [OrganizationController::class, 'landing'])->name('index');

        // ============================================
        // Organization Details Routes
        // ============================================
        Route::resource('details', OrganizationController::class)
            ->parameters(['details' => 'organization'])
            ->names([
                'index' => 'details.index',
                'create' => 'details.create',
                'store' => 'details.store',
                'show' => 'details.show',
                'edit' => 'details.edit',
                'update' => 'details.update',
                'destroy' => 'details.destroy',
            ]);

        // ============================================
        // Leadership Positions Routes
        // ============================================
        Route::resource('leadership-positions', LeadershipPositionController::class)
            ->parameters(['leadership-positions' => 'leadershipPosition'])
            ->except(['show']);

        // Leadership assignment routes
        Route::get('leadership-positions/{leadershipPosition}/assign', [LeadershipPositionController::class, 'assign'])
            ->name('leadership-positions.assign');
        Route::post('leadership-positions/{leadershipPosition}/assign', [LeadershipPositionController::class, 'storeAssignment'])
            ->name('leadership-positions.store-assignment');

        // Leadership unassignment route
        Route::delete('leadership-assignments/{assignment}', [LeadershipPositionController::class, 'unassign'])
            ->name('leadership-assignments.destroy');

        // ============================================
        // Election Cycles Routes
        // ============================================

        // AJAX route for getting positions by organization — MUST be before resource routes
        Route::get('election-cycles/positions/{organization}', [ElectionCycleController::class, 'getPositions'])
            ->name('election-cycles.positions');

        // Election cycle completion route — MUST be before resource routes
        Route::post('election-cycles/{electionCycle}/complete', [ElectionCycleController::class, 'complete'])
            ->name('election-cycles.complete');

        Route::resource('election-cycles', ElectionCycleController::class);

        // ============================================
        // Voting Procedures Routes
        // ============================================

        // AJAX route for getting election cycles by organization — MUST be before resource routes
        Route::get('voting-procedures/election-cycles/{organization}', [VotingProcedureController::class, 'getElectionCycles'])
            ->name('voting-procedures.election-cycles');

        Route::resource('voting-procedures', VotingProcedureController::class);

        // ============================================
        // Leadership History Routes (Read-only + Achievements)
        // ============================================
        Route::get('leadership-history', [LeadershipHistoryController::class, 'index'])
            ->name('leadership-history.index');
        Route::get('leadership-history/{leadershipHistory}', [LeadershipHistoryController::class, 'show'])
            ->name('leadership-history.show');

        // Achievement routes for leadership history
        Route::post('leadership-history/{leadershipHistory}/achievements', [LeadershipHistoryController::class, 'addAchievement'])
            ->name('leadership-history.achievements.store');
        Route::delete('leadership-history/achievements/{achievement}', [LeadershipHistoryController::class, 'deleteAchievement'])
            ->name('leadership-history.achievements.destroy');

        // ============================================
        // Staff History Routes
        // ============================================
        Route::resource('staff-history', StaffHistoryController::class);

        // Achievement routes for staff history
        Route::post('staff-history/{staffHistory}/achievements', [StaffHistoryController::class, 'addAchievement'])
            ->name('staff-history.achievements.store');
        Route::delete('staff-history/achievements/{achievement}', [StaffHistoryController::class, 'deleteAchievement'])
            ->name('staff-history.achievements.destroy');

        // Passport routes for staff history
        Route::delete('staff-history/{staffHistory}/passport', [StaffHistoryController::class, 'deletePassport'])
            ->name('staff-history.delete-passport');

        // ============================================
        // Organogram Routes
        // ============================================

        // AJAX routes for organogram data — MUST be before the show route
        Route::get('organogram/hierarchy/{organization}', [OrganogramController::class, 'getHierarchy'])
            ->name('organogram.hierarchy');
        Route::get('organogram/flat-list/{organization}', [OrganogramController::class, 'getFlatList'])
            ->name('organogram.flat-list');
        Route::get('organogram/export/{format}', [OrganogramController::class, 'export'])
            ->name('organogram.export')
            ->where('format', 'pdf|png|svg|json');

        Route::get('organogram', [OrganogramController::class, 'show'])
            ->name('organogram.show');
    });
