<?php

use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\TwoFactorController;
use App\Http\Controllers\Customer\AccountController as CustomerAccountController;
use App\Http\Controllers\Customer\AuthController as CustomerAuthController;
use App\Http\Controllers\Panel\AdminController;
use App\Http\Controllers\Panel\ApplicationController as PanelApplicationController;
use App\Http\Controllers\Panel\CategoryController;
use App\Http\Controllers\Panel\CompanyController;
use App\Http\Controllers\Panel\CustomerController as PanelCustomerController;
use App\Http\Controllers\Panel\DashboardController;
use App\Http\Controllers\Panel\DealerController;
use App\Http\Controllers\Panel\PackageController;
use App\Http\Controllers\Panel\PaymentController;
use App\Http\Controllers\Panel\PostController;
use App\Http\Controllers\Panel\ReportController;
use App\Http\Controllers\Panel\RoleController;
use App\Http\Controllers\Panel\SettingController;
use App\Http\Controllers\Panel\SubscriptionController;
use App\Http\Controllers\Site\ApplicationController as SiteApplicationController;
use App\Http\Controllers\Site\HomeController;
use App\Http\Controllers\Site\PackageSiteController;
use App\Http\Controllers\Site\PostSiteController;
use Illuminate\Support\Facades\Route;

Route::get('/', [HomeController::class, 'index'])->name('home');
Route::get('/paketler', [PackageSiteController::class, 'index'])->name('packages.index');
Route::get('/karsilastir', [PackageSiteController::class, 'compare'])->name('packages.compare');
Route::get('/paketler/{package:slug}', [PackageSiteController::class, 'show'])->name('packages.show');
Route::post('/basvuru', [SiteApplicationController::class, 'store'])->name('applications.store');
Route::get('/blog', [PostSiteController::class, 'blogIndex'])->name('blog.index');
Route::get('/blog/{post:slug}', [PostSiteController::class, 'show'])->name('posts.show');
Route::get('/sss', [PostSiteController::class, 'faq'])->name('faq');

// Bireysel müşteri (pazarlama sitesi) giriş/kayıt — bayi/personel girişinden ayrı guard.
Route::prefix('musteri')->name('customer.')->group(function () {
    Route::middleware('guest:customer')->group(function () {
        Route::get('/giris', [CustomerAuthController::class, 'create'])->name('login');
        Route::post('/giris', [CustomerAuthController::class, 'login'])->name('login.store');
        Route::post('/kayit', [CustomerAuthController::class, 'register'])->name('register.store');
    });
    Route::middleware('auth:customer')->group(function () {
        Route::get('/hesabim', [CustomerAccountController::class, 'index'])->name('account');
        Route::post('/sifre-guncelle', [CustomerAccountController::class, 'updatePassword'])->name('password.update');
        Route::post('/cikis', [CustomerAuthController::class, 'logout'])->name('logout');
    });
});

// Bayi/personel paneli girişi (pazarlama sitesi üst menüsünde artık gösterilmiyor,
// URL aynı kalır: /giris — mevcut bayi/admin kullanıcıları bu adresi kullanmaya devam eder).
Route::middleware('guest')->group(function () {
    Route::get('/giris', [LoginController::class, 'create'])->name('login');
    Route::post('/giris', [LoginController::class, 'store'])->name('login.store');
    Route::get('/iki-faktor/dogrula', [TwoFactorController::class, 'challenge'])->name('two-factor.challenge');
    Route::post('/iki-faktor/dogrula', [TwoFactorController::class, 'verify'])->name('two-factor.verify');
});
Route::post('/cikis', [LoginController::class, 'destroy'])->middleware('auth')->name('logout');

Route::middleware(['auth'])->prefix('panel')->name('panel.')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])
        ->middleware('permission:dashboard.view')->name('dashboard');

    Route::get('/iki-faktor/kurulum', [TwoFactorController::class, 'setup'])->name('two-factor.setup');
    Route::post('/iki-faktor/onayla', [TwoFactorController::class, 'confirm'])->name('two-factor.confirm');

    Route::middleware('permission:dealers.view')->group(function () {
        Route::get('dealers-data', [DealerController::class, 'data'])->name('dealers.data');
        Route::resource('dealers', DealerController::class);
        Route::post('dealers/{dealer}/toggle-status', [DealerController::class, 'toggleStatus'])->name('dealers.toggle-status');
        Route::post('dealers/{dealer}/commission', [DealerController::class, 'storeCommission'])
            ->middleware('permission:dealers.manage-commission')->name('dealers.commission.store');
    });

    Route::middleware('permission:subscriptions.view')->group(function () {
        Route::get('subscriptions-data', [SubscriptionController::class, 'data'])->name('subscriptions.data');
        Route::resource('subscriptions', SubscriptionController::class);
        Route::patch('subscriptions/{subscription}/status', [SubscriptionController::class, 'updateStatus'])
            ->middleware('permission:subscriptions.change-status')->name('subscriptions.update-status');
    });

    Route::middleware('permission:companies.view')->group(fn () => Route::resource('companies', CompanyController::class));
    Route::middleware('permission:packages.view')->group(fn () => Route::resource('packages', PackageController::class));

    Route::middleware('permission:finance.view')->group(function () {
        Route::resource('payments', PaymentController::class)->only(['index', 'create', 'store']);
    });

    Route::middleware('permission:content.categories.view')->group(fn () => Route::resource('categories', CategoryController::class)->except(['show']));
    Route::middleware('permission:content.posts.view')->group(fn () => Route::resource('posts', PostController::class));

    Route::middleware('permission:applications.view')->group(function () {
        Route::get('applications', [PanelApplicationController::class, 'index'])->name('applications.index');
        Route::patch('applications/{application}/status', [PanelApplicationController::class, 'updateStatus'])
            ->middleware('permission:applications.assign')->name('applications.update-status');
        Route::post('applications/{application}/convert', [PanelApplicationController::class, 'convert'])
            ->middleware('permission:applications.convert')->name('applications.convert');
    });

    Route::middleware('permission:reports.view')->group(function () {
        Route::get('/reports', [ReportController::class, 'index'])->name('reports.index');
        Route::get('/reports/export/excel', [ReportController::class, 'exportExcel'])->name('reports.export.excel');
        Route::get('/reports/export/csv', [ReportController::class, 'exportCsv'])->name('reports.export.csv');
        Route::get('/reports/export/pdf', [ReportController::class, 'exportPdf'])->name('reports.export.pdf');
    });

    Route::middleware('permission:users.view')->group(fn () => Route::resource('admins', AdminController::class)->except(['show'])->parameters(['admins' => 'admin']));
    Route::middleware('permission:customers.view')->group(function () {
        Route::get('customers', [PanelCustomerController::class, 'index'])->name('customers.index');
        Route::delete('customers/{customer}', [PanelCustomerController::class, 'destroy'])
            ->middleware('permission:customers.delete')->name('customers.destroy');
    });
    Route::middleware('permission:roles.view')->group(fn () => Route::resource('roles', RoleController::class)->only(['index', 'edit', 'update']));
    Route::middleware('permission:settings.manage')->group(function () {
        Route::get('/settings', [SettingController::class, 'index'])->name('settings.index');
        Route::post('/settings', [SettingController::class, 'update'])->name('settings.update');
    });
});
