<?php

use App\Http\Controllers\Admin;
use App\Http\Controllers\Client;
use App\Http\Controllers\ContactController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\MeetingController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Frontend (нийтийн) хуудаснууд
|--------------------------------------------------------------------------
*/

Route::get('/', [HomeController::class, 'index'])->name('home');

Route::get('/about', [HomeController::class, 'about'])->name('about');

Route::get('/services',          [HomeController::class, 'services'])->name('services.index');
Route::get('/services/{slug}',   [HomeController::class, 'serviceShow'])->name('services.show');

Route::get('/portfolio',         [HomeController::class, 'portfolio'])->name('portfolio.index');
Route::get('/portfolio/{slug}',  [HomeController::class, 'portfolioShow'])->name('portfolio.show');

Route::get('/news',              [HomeController::class, 'news'])->name('news.index');
Route::get('/news/{slug}',       [HomeController::class, 'newsShow'])->name('news.show');

Route::get('/contact',  [ContactController::class, 'index'])->name('contact');
Route::post('/contact', [ContactController::class, 'store'])->name('contact.store');

Route::get('/meeting',  [MeetingController::class, 'create'])->name('meeting.create');
Route::post('/meeting', [MeetingController::class, 'store'])->name('meeting.store');
Route::get('/meeting/slots', [MeetingController::class, 'availableSlots'])->name('meeting.slots');

/*
|--------------------------------------------------------------------------
| Шинэ: Гарын авлага, Survey, Templates
|--------------------------------------------------------------------------
*/
Route::get('/guide',           [\App\Http\Controllers\GuideController::class, 'show'])->name('guide');
Route::post('/guide/download', [\App\Http\Controllers\GuideController::class, 'download'])->name('guide.download');

Route::get('/survey',          [\App\Http\Controllers\SurveyController::class, 'show'])->name('survey');
Route::post('/survey/submit',  [\App\Http\Controllers\SurveyController::class, 'submit'])->name('survey.submit');
Route::get('/survey/insights/{id}', [\App\Http\Controllers\SurveyController::class, 'insights'])->name('survey.insights');

Route::get('/templates',       [\App\Http\Controllers\TemplatesController::class, 'index'])->name('templates');
Route::post('/templates/order',[\App\Http\Controllers\TemplatesController::class, 'order'])->name('templates.order');

Route::get('/web-services',         [\App\Http\Controllers\WebServicesController::class, 'index'])->name('web-services');
Route::post('/web-services/inquire',[\App\Http\Controllers\WebServicesController::class, 'inquire'])->name('web-services.inquire');

/*
|--------------------------------------------------------------------------
| QPay Payment
|--------------------------------------------------------------------------
*/
Route::post('/payment/create',          [\App\Http\Controllers\PaymentController::class, 'create'])->name('payment.create');
Route::get('/payment/{payment}',        [\App\Http\Controllers\PaymentController::class, 'show'])->name('payment.show');
Route::get('/payment/{payment}/status', [\App\Http\Controllers\PaymentController::class, 'status'])->name('payment.status');
Route::post('/payment/webhook/qpay',    [\App\Http\Controllers\PaymentController::class, 'webhook'])->name('payment.webhook')->withoutMiddleware(['web']);


/*
|--------------------------------------------------------------------------
| Харилцагчийн портал
|--------------------------------------------------------------------------
*/
Route::prefix('client')->name('client.')->group(function () {
    // Auth
    Route::get('login',       [Client\AuthController::class, 'showLogin'])->name('login');
    Route::post('login',      [Client\AuthController::class, 'login'])->name('login.post');
    Route::get('register',    [Client\AuthController::class, 'showRegister'])->name('register');
    Route::post('register',   [Client\AuthController::class, 'register'])->name('register.post');
    Route::post('logout',     [Client\AuthController::class, 'logout'])->name('logout');

    // Логин шаардсан
    Route::middleware('client')->group(function () {
        Route::get('/',           [Client\DashboardController::class, 'index'])->name('dashboard');
        Route::get('dashboard',   [Client\DashboardController::class, 'index']);
        Route::get('profile',     [Client\DashboardController::class, 'profile'])->name('profile');
        Route::put('profile',     [Client\DashboardController::class, 'updateProfile'])->name('profile.update');

        // Захиалга
        Route::get('orders',                  [Client\OrderController::class, 'index'])->name('orders.index');
        Route::get('orders/create',           [Client\OrderController::class, 'create'])->name('orders.create');
        Route::post('orders',                 [Client\OrderController::class, 'store'])->name('orders.store');
        Route::get('orders/{order}',          [Client\OrderController::class, 'show'])->name('orders.show');
        Route::post('orders/{order}/messages',[Client\OrderController::class, 'addMessage'])->name('orders.messages');
        Route::post('orders/{order}/cancel',  [Client\OrderController::class, 'cancel'])->name('orders.cancel');
    });
});

/*
|--------------------------------------------------------------------------
| Админ панел
|--------------------------------------------------------------------------
*/
Route::prefix('admin')->name('admin.')->group(function () {
    // Auth
    Route::get('login',  [Admin\AuthController::class, 'showLogin'])->name('login');
    Route::post('login', [Admin\AuthController::class, 'login'])->name('login.post');
    Route::post('logout',[Admin\AuthController::class, 'logout'])->name('logout');

    Route::middleware('admin')->group(function () {
        // Dashboard
        Route::get('/',          [Admin\DashboardController::class, 'index'])->name('dashboard');
        Route::get('dashboard',  [Admin\DashboardController::class, 'index']);

        // Services
        Route::resource('services', Admin\ServiceController::class)->except('show');
        Route::post('services/{service}/toggle',  [Admin\ServiceController::class, 'toggle'])->name('services.toggle');
        Route::post('services/reorder',           [Admin\ServiceController::class, 'reorder'])->name('services.reorder');

        // Portfolios
        Route::resource('portfolios', Admin\PortfolioController::class)->except('show');
        Route::post('portfolios/{portfolio}/toggle', [Admin\PortfolioController::class, 'toggle'])->name('portfolios.toggle');

        // Partners
        Route::resource('partners', Admin\PartnerController::class)->except('show');

        // News
        Route::resource('news', Admin\NewsController::class)->except('show');

        // Heroes
        Route::resource('heroes', Admin\HeroController::class)->except('show');

        // Clients (CRM)
        Route::resource('clients', Admin\ClientController::class);
        Route::post('clients/{client}/status', [Admin\ClientController::class, 'updateStatus'])->name('clients.status');
        Route::post('clients/{client}/notes',  [Admin\ClientController::class, 'updateNotes'])->name('clients.notes');

        // Meetings
        Route::get('meetings',                 [Admin\MeetingController::class, 'index'])->name('meetings.index');
        Route::get('meetings/{meeting}',       [Admin\MeetingController::class, 'show'])->name('meetings.show');
        Route::put('meetings/{meeting}',       [Admin\MeetingController::class, 'update'])->name('meetings.update');
        Route::delete('meetings/{meeting}',    [Admin\MeetingController::class, 'destroy'])->name('meetings.destroy');

        // Orders (work order CRM)
        Route::get('orders',                          [Admin\OrderController::class, 'index'])->name('orders.index');
        Route::get('orders/{order}',                  [Admin\OrderController::class, 'show'])->name('orders.show');
        Route::put('orders/{order}',                  [Admin\OrderController::class, 'update'])->name('orders.update');
        Route::delete('orders/{order}',               [Admin\OrderController::class, 'destroy'])->name('orders.destroy');
        Route::post('orders/{order}/status',          [Admin\OrderController::class, 'changeStatus'])->name('orders.status');
        Route::post('orders/{order}/messages',        [Admin\OrderController::class, 'addMessage'])->name('orders.messages');

        // Contact messages
        Route::get('messages',              [Admin\MessageController::class, 'index'])->name('messages.index');
        Route::get('messages/{message}',    [Admin\MessageController::class, 'show'])->name('messages.show');
        Route::post('messages/{message}/status', [Admin\MessageController::class, 'updateStatus'])->name('messages.status');
        Route::delete('messages/{message}', [Admin\MessageController::class, 'destroy'])->name('messages.destroy');


        // Leads
        Route::get('leads',                       [Admin\LeadController::class, 'index'])->name('leads.index');
        Route::get('leads/export',                [Admin\LeadController::class, 'export'])->name('leads.export');
        Route::get('leads/{lead}',                [Admin\LeadController::class, 'show'])->name('leads.show');
        Route::post('leads/{lead}/status',        [Admin\LeadController::class, 'updateStatus'])->name('leads.status');
        Route::post('leads/{lead}/notes',         [Admin\LeadController::class, 'updateNotes'])->name('leads.notes');

        // Settings
        Route::get('settings',  [Admin\SettingController::class, 'edit'])->name('settings.edit');
        Route::post('settings', [Admin\SettingController::class, 'update'])->name('settings.update');
    });
});
