<?php

use App\Http\Controllers\CustomerController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\LeadController;
use App\Http\Controllers\LeadExportController;
use App\Http\Controllers\LeadStageController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\PipelineController;
use App\Http\Controllers\ReportsController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\TaskController;
use App\Http\Controllers\TeamController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return auth()->check()
        ? redirect()->route('dashboard')
        : view('welcome');
});

Route::middleware(['auth', 'company'])->group(function () {
    Route::get('/dashboard', DashboardController::class)->name('dashboard');

    Route::get('/pipeline', PipelineController::class)->name('pipeline.index');
    Route::post('/leads/{lead}/stage', [LeadStageController::class, 'update'])->name('leads.stage');

    Route::get('/leads/export', LeadExportController::class)->name('leads.export');
    Route::post('/leads/{lead}/duplicate', [LeadController::class, 'duplicate'])->name('leads.duplicate');
    Route::resource('leads', LeadController::class);

    Route::get('/reports', ReportsController::class)->name('reports.index');

    Route::get('/settings', [SettingsController::class, 'edit'])->name('settings.edit');
    Route::put('/settings', [SettingsController::class, 'update'])->name('settings.update');

    Route::get('/team', [TeamController::class, 'index'])->name('team.index');
    Route::get('/team/create', [TeamController::class, 'create'])->name('team.create');
    Route::post('/team', [TeamController::class, 'store'])->name('team.store');
    Route::get('/team/{member}/edit', [TeamController::class, 'edit'])->name('team.edit');
    Route::put('/team/{member}', [TeamController::class, 'update'])->name('team.update');
    Route::delete('/team/{member}', [TeamController::class, 'destroy'])->name('team.destroy');

    Route::get('/customers', [CustomerController::class, 'index'])->name('customers.index');
    Route::get('/customers/{customer}', [CustomerController::class, 'show'])->name('customers.show');
    Route::put('/customers/{customer}', [CustomerController::class, 'update'])->name('customers.update');
    Route::post('/customers/{customer}/activities', [CustomerController::class, 'storeActivity'])->name('customers.activities.store');
    Route::post('/leads/{lead}/convert', [CustomerController::class, 'storeFromLead'])->name('customers.from-lead');

    Route::get('/tasks', [TaskController::class, 'index'])->name('tasks.index');
    Route::get('/tasks/create', [TaskController::class, 'create'])->name('tasks.create');
    Route::post('/tasks', [TaskController::class, 'store'])->name('tasks.store');
    Route::get('/tasks/{task}/edit', [TaskController::class, 'edit'])->name('tasks.edit');
    Route::put('/tasks/{task}', [TaskController::class, 'update'])->name('tasks.update');
    Route::delete('/tasks/{task}', [TaskController::class, 'destroy'])->name('tasks.destroy');
    Route::post('/tasks/{task}/complete', [TaskController::class, 'complete'])->name('tasks.complete');

    Route::get('/notifications', [NotificationController::class, 'index'])->name('notifications.index');
    Route::post('/notifications/{id}/read', [NotificationController::class, 'read'])->name('notifications.read');
    Route::post('/notifications/read-all', [NotificationController::class, 'readAll'])->name('notifications.read-all');

    // Clinic resources & actions
    Route::get('/patients/scan/{token}', [App\Http\Controllers\PatientQrController::class, 'scan'])->name('patients.scan');
    Route::get('/patients/{patient}/print-card', [App\Http\Controllers\PatientController::class, 'printCard'])->name('patients.print-card');
    Route::get('/patients/{patient}/print-file', [App\Http\Controllers\PatientController::class, 'printFile'])->name('patients.print-file');
    Route::resource('patients', App\Http\Controllers\PatientController::class);
    Route::resource('visits', App\Http\Controllers\VisitController::class);
    Route::resource('invoices', App\Http\Controllers\InvoiceController::class);
});

Route::get('/locale/{lang}', function ($lang) {
    if (in_array($lang, ['ar', 'en'])) {
        session(['locale' => $lang]);
    }
    return redirect()->back();
})->name('locale.switch');

Route::get('/debug-lang', function () {
    $opcache_cleared = false;
    if (function_exists('opcache_reset')) {
        $opcache_cleared = opcache_reset();
    }
    return [
        'opcache_cleared' => $opcache_cleared,
        'locale' => app()->getLocale(),
        'lang_path' => app()->langPath(),
        'file_exists_ar' => file_exists(app()->langPath() . '/ar/clinic.php'),
        'resolved_ar' => trans('clinic.dashboard', [], 'ar'),
        'resolved_current' => trans('clinic.dashboard'),
    ];
});

require __DIR__.'/auth.php';
