<?php

use App\Http\Controllers\Admin\ActivityLogController;
use App\Http\Controllers\Admin\Auth\LoginController;
use App\Http\Controllers\Admin\CityController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\FeasibilityController;
use App\Http\Controllers\Admin\RegulationController;
use App\Http\Controllers\Admin\RegulationDocumentController;
use App\Http\Controllers\Admin\RoleController;
use App\Http\Controllers\Admin\SettingsController;
use App\Http\Controllers\Admin\StateController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Admin\ZoneController;
use App\Http\Controllers\Admin\ZoneRuleController;
use App\Http\Controllers\Portal\AnalyzeController;
use App\Http\Controllers\Portal\CompareController;
use App\Http\Controllers\Portal\ReportController;
use App\Http\Controllers\Portal\SurveyController;
use Illuminate\Support\Facades\Route;

// ─────────────────────────────────────────────────────────────────────────────
// Public portal
// ─────────────────────────────────────────────────────────────────────────────
Route::get('/', [AnalyzeController::class, 'index'])->name('portal.home');
Route::get('/analyze', [AnalyzeController::class, 'index'])->name('portal.analyze.form');
Route::post('/analyze', [AnalyzeController::class, 'submit'])->name('portal.analyze');
Route::get('/report/{id}', [AnalyzeController::class, 'report'])->name('portal.report');
Route::get('/report/{id}/pdf', [ReportController::class, 'pdf'])->name('portal.report.pdf');

// Plot survey upload + AI analysis (portal)
Route::get('/survey/upload', [SurveyController::class, 'create'])->name('portal.survey.upload');
Route::post('/survey', [SurveyController::class, 'store'])->name('portal.survey.store');
Route::get('/survey/{survey}', [SurveyController::class, 'show'])->name('portal.survey.show');
Route::post('/survey/{survey}/confirm', [SurveyController::class, 'confirm'])->name('portal.survey.confirm');
Route::post('/survey/{survey}/reanalyze', [SurveyController::class, 'reanalyze'])->name('portal.survey.reanalyze');

// Compare tool
Route::get('/compare', [CompareController::class, 'index'])->name('portal.compare');
Route::post('/compare', [CompareController::class, 'compare'])->name('portal.compare.submit');

// AJAX helpers (no auth required — read-only reference data)
Route::get('/ajax/cities/{stateId}', [AnalyzeController::class, 'cities'])->name('ajax.cities');
Route::get('/ajax/zones/{stateId}', [AnalyzeController::class, 'zones'])->name('ajax.zones');
Route::get('/ajax/geocode', [AnalyzeController::class, 'geocode'])->name('ajax.geocode');
Route::get('/ajax/zone-preview/{zoneId}', [AnalyzeController::class, 'zonePreview'])->name('ajax.zone-preview');
Route::get('/ajax/resolve-map-url', [AnalyzeController::class, 'resolveMapUrl'])->name('ajax.resolve-map-url');

// ─────────────────────────────────────────────────────────────────────────────
// Admin Auth
// ─────────────────────────────────────────────────────────────────────────────
Route::middleware('guest')->group(function () {
    Route::get('/admin/login', [LoginController::class, 'showLoginForm'])->name('admin.login');
    Route::post('/admin/login', [LoginController::class, 'login'])->name('admin.login.post');
});
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

// ─────────────────────────────────────────────────────────────────────────────
// Admin Protected
// ─────────────────────────────────────────────────────────────────────────────
Route::prefix('admin')->name('admin.')->middleware('admin.auth')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // Users & Roles
    Route::resource('users', UserController::class);
    Route::resource('roles', RoleController::class);

    // Land Data
    Route::resource('states', StateController::class);
    Route::resource('cities', CityController::class);
    Route::resource('zones', ZoneController::class);
    Route::resource('zone-rules', ZoneRuleController::class);
    Route::resource('regulations', RegulationController::class);

    // AI Regulation Documents
    Route::resource('regulation-documents', RegulationDocumentController::class)->except(['edit', 'update']);
    Route::post('/regulation-documents/{regulationDocument}/reprocess',   [RegulationDocumentController::class, 'reprocess'])->name('regulation-documents.reprocess');
    Route::post('/regulation-documents/{regulationDocument}/review',      [RegulationDocumentController::class, 'review'])->name('regulation-documents.review');
    Route::post('/regulation-documents/{regulationDocument}/apply-rules', [RegulationDocumentController::class, 'applyRules'])->name('regulation-documents.apply-rules');

    // Feasibility Reports
    Route::resource('feasibility', FeasibilityController::class)->only(['index', 'show', 'destroy']);
    Route::get('/feasibility/{id}/pdf', [FeasibilityController::class, 'pdf'])->name('feasibility.pdf');

    // Settings & Logs
    Route::get('/settings', [SettingsController::class, 'index'])->name('settings');
    Route::put('/settings', [SettingsController::class, 'update'])->name('settings.update');
    Route::delete('/settings/api-key', [SettingsController::class, 'clearApiKey'])->name('settings.clear-api-key');
    Route::get('/activity-log', [ActivityLogController::class, 'index'])->name('activity-log');
});

