<?php
/**
 * SalonPro X - Application Bootstrap
 * Initializes core components and dispatches the request
 */

namespace core;

class App
{
    private Router $router;
    private Request $request;

    public function __construct()
    {
        $this->router = new Router();
        $this->request = Request::capture();

        // Initialize authentication
        Auth::init();

        // Register global middleware
        $this->registerGlobalMiddleware();

        // Register all routes
        $this->registerRoutes();
    }

    /**
     * Run the application
     */
    public function run(): void
    {
        $this->router->dispatch($this->request);
    }

    /**
     * Register global middleware that runs on every request
     */
    private function registerGlobalMiddleware(): void
    {
        $this->router->addGlobalMiddleware(\middleware\ActivityMiddleware::class);
    }

    /**
     * Register all application routes
     */
    private function registerRoutes(): void
    {
        // Authentication routes
        $this->router->get('/login', 'AuthController@loginForm');
        $this->router->post('/login', 'AuthController@login');
        $this->router->get('/logout', 'AuthController@logout');
        $this->router->get('/forgot-password', 'AuthController@forgotPasswordForm');
        $this->router->post('/forgot-password', 'AuthController@forgotPassword');
        $this->router->get('/reset-password/{token}', 'AuthController@resetPasswordForm');
        $this->router->post('/reset-password', 'AuthController@resetPassword');

        // Dashboard
        $this->router->get('/', 'DashboardController@index');
        $this->router->get('/dashboard', 'DashboardController@index');

        // Profile
        $this->router->get('/profile', 'AuthController@profile');
        $this->router->post('/profile/update', 'AuthController@updateProfile');
        $this->router->post('/profile/password', 'AuthController@changePassword');

        // Role Management
        $this->router->get('/roles', 'RoleController@index');
        $this->router->get('/roles/create', 'RoleController@create');
        $this->router->post('/roles/store', 'RoleController@store');
        $this->router->get('/roles/edit/{id}', 'RoleController@edit');
        $this->router->post('/roles/update/{id}', 'RoleController@update');
        $this->router->post('/roles/delete/{id}', 'RoleController@delete');

        // User Management
        $this->router->get('/users', 'UserController@index');
        $this->router->get('/users/create', 'UserController@create');
        $this->router->post('/users/store', 'UserController@store');
        $this->router->get('/users/edit/{id}', 'UserController@edit');
        $this->router->post('/users/update/{id}', 'UserController@update');
        $this->router->post('/users/delete/{id}', 'UserController@delete');

        // Branch Management
        $this->router->get('/branches', 'BranchController@index');
        $this->router->get('/branches/create', 'BranchController@create');
        $this->router->post('/branches/store', 'BranchController@store');
        $this->router->get('/branches/edit/{id}', 'BranchController@edit');
        $this->router->post('/branches/update/{id}', 'BranchController@update');
        $this->router->post('/branches/delete/{id}', 'BranchController@delete');

        // Customer Management
        $this->router->get('/customers', 'CustomerController@index');
        $this->router->get('/customers/create', 'CustomerController@create');
        $this->router->post('/customers/store', 'CustomerController@store');
        $this->router->get('/customers/edit/{id}', 'CustomerController@edit');
        $this->router->post('/customers/update/{id}', 'CustomerController@update');
        $this->router->get('/customers/show/{id}', 'CustomerController@show');
        $this->router->post('/customers/delete/{id}', 'CustomerController@delete');
        $this->router->get('/customers/search', 'CustomerController@search');
        $this->router->post('/customers/{id}/photo', 'CustomerController@uploadPhoto');
        $this->router->post('/customers/{id}/notes', 'CustomerController@addNote');
        $this->router->get('/customers/{id}/history', 'CustomerController@history');

        // Appointment Management
        $this->router->get('/appointments', 'AppointmentController@index');
        $this->router->get('/appointments/create', 'AppointmentController@create');
        $this->router->post('/appointments/store', 'AppointmentController@store');
        $this->router->get('/appointments/edit/{id}', 'AppointmentController@edit');
        $this->router->post('/appointments/update/{id}', 'AppointmentController@update');
        $this->router->post('/appointments/delete/{id}', 'AppointmentController@delete');
        $this->router->post('/appointments/status/{id}', 'AppointmentController@updateStatus');
        $this->router->get('/appointments/calendar', 'CalendarController@index');
        $this->router->get('/appointments/calendar/events', 'CalendarController@events');
        $this->router->get('/appointments/show/{id}', 'AppointmentController@show');
        $this->router->get('/appointments/{id}', 'AppointmentController@show');
        $this->router->post('/appointments/checkin/{id}', 'AppointmentController@checkIn');
        $this->router->get('/reception', 'AppointmentController@reception');

        // Service Management
        $this->router->get('/services', 'ServiceController@index');
        $this->router->get('/services/create', 'ServiceController@create');
        $this->router->post('/services/store', 'ServiceController@store');
        $this->router->get('/services/edit/{id}', 'ServiceController@edit');
        $this->router->post('/services/update/{id}', 'ServiceController@update');
        $this->router->post('/services/delete/{id}', 'ServiceController@delete');
        $this->router->post('/services/search', 'ServiceController@search');

        // Service Categories
        $this->router->get('/services/categories', 'CategoryController@index');
        $this->router->post('/services/categories/store', 'CategoryController@store');
        $this->router->post('/services/categories/update/{id}', 'CategoryController@update');
        $this->router->post('/services/categories/delete/{id}', 'CategoryController@delete');

        // Packages
        $this->router->get('/packages', 'PackageController@index');
        $this->router->get('/packages/create', 'PackageController@create');
        $this->router->post('/packages/store', 'PackageController@store');
        $this->router->get('/packages/edit/{id}', 'PackageController@edit');
        $this->router->post('/packages/update/{id}', 'PackageController@update');
        $this->router->post('/packages/delete/{id}', 'PackageController@delete');

        // POS
        $this->router->get('/pos', 'POSController@index');
        $this->router->get('/pos/new', 'POSController@newSale');
        $this->router->post('/pos/calculate', 'POSController@calculate');
        $this->router->post('/pos/checkout', 'POSController@checkout');
        $this->router->post('/pos/validate-coupon', 'POSController@validateCoupon');
        $this->router->get('/pos/load-appointment/{id}', 'POSController@loadAppointment');
        $this->router->get('/pos/invoice/{id}', 'POSController@invoice');
        $this->router->get('/pos/invoice/{id}/print', 'POSController@printInvoice');

        // Invoices
        $this->router->get('/invoices', 'InvoiceController@index');
        $this->router->get('/invoices/{id}', 'InvoiceController@show');
        $this->router->post('/invoices/{id}/payment', 'PaymentController@store');
        $this->router->get('/invoices/{id}/print', 'InvoiceController@printInvoice');
        $this->router->post('/invoices/{id}/refund', 'RefundController@store');

        // Payments
        $this->router->get('/payments', 'PaymentController@index');

        // Refunds
        $this->router->get('/refunds', 'RefundController@index');

        // Employee Management
        $this->router->get('/employees', 'EmployeeController@index');
        $this->router->get('/employees/create', 'EmployeeController@create');
        $this->router->post('/employees/store', 'EmployeeController@store');
        $this->router->get('/employees/edit/{id}', 'EmployeeController@edit');
        $this->router->post('/employees/update/{id}', 'EmployeeController@update');
        $this->router->get('/employees/show/{id}', 'EmployeeController@show');
        $this->router->post('/employees/delete/{id}', 'EmployeeController@delete');

        // Attendance
        $this->router->get('/attendance', 'AttendanceController@index');
        $this->router->post('/attendance/checkin', 'AttendanceController@checkIn');
        $this->router->post('/attendance/checkout/{id}', 'AttendanceController@checkOut');
        $this->router->get('/attendance/report', 'AttendanceController@report');

        // Commissions
        $this->router->get('/commissions', 'CommissionController@index');
        $this->router->post('/commissions/calculate', 'CommissionController@calculate');
        $this->router->post('/commissions/pay/{id}', 'CommissionController@pay');

        // Salary
        $this->router->get('/salary', 'SalaryController@index');
        $this->router->get('/salary/create', 'SalaryController@create');
        $this->router->post('/salary/store', 'SalaryController@store');
        $this->router->post('/salary/pay/{id}', 'SalaryController@pay');
        $this->router->get('/salary/payslip/{id}', 'SalaryController@payslip');

        // Performance
        $this->router->get('/performance', 'PerformanceController@index');
        $this->router->post('/performance/store', 'PerformanceController@store');

        // Inventory / Products
        $this->router->get('/inventory', 'ProductController@index');
        $this->router->get('/inventory/create', 'ProductController@create');
        $this->router->post('/inventory/store', 'ProductController@store');
        $this->router->get('/inventory/edit/{id}', 'ProductController@edit');
        $this->router->post('/inventory/update/{id}', 'ProductController@update');
        $this->router->post('/inventory/delete/{id}', 'ProductController@delete');
        $this->router->get('/inventory/{id}', 'ProductController@show');
        $this->router->get('/inventory/show/{id}', 'ProductController@show');
        $this->router->post('/inventory/search', 'ProductController@search');
        $this->router->get('/inventory/low-stock', 'ProductController@lowStock');
        $this->router->get('/inventory/expiring', 'ProductController@expiring');
        $this->router->post('/inventory/batches/store', 'ProductController@batchStore');
        $this->router->post('/inventory/batches/dispose/{id}', 'ProductController@batchDispose');

        // Suppliers
        $this->router->get('/suppliers', 'SupplierController@index');
        $this->router->get('/suppliers/create', 'SupplierController@create');
        $this->router->post('/suppliers/store', 'SupplierController@store');
        $this->router->get('/suppliers/edit/{id}', 'SupplierController@edit');
        $this->router->post('/suppliers/update/{id}', 'SupplierController@update');
        $this->router->post('/suppliers/delete/{id}', 'SupplierController@delete');

        // Purchase Orders
        $this->router->get('/purchases', 'PurchaseController@index');
        $this->router->get('/purchases/create', 'PurchaseController@create');
        $this->router->post('/purchases/store', 'PurchaseController@store');
        $this->router->get('/purchases/show/{id}', 'PurchaseController@show');
        $this->router->get('/purchases/{id}', 'PurchaseController@show');
        $this->router->post('/purchases/approve/{id}', 'PurchaseController@approve');
        $this->router->post('/purchases/receive/{id}', 'PurchaseController@receive');

        // Goods Received Notes
        $this->router->get('/purchases/grn', 'PurchaseController@grnList');
        $this->router->get('/purchases/grn/{id}', 'PurchaseController@grnView');

        // Stock Transfer
        $this->router->get('/stock-transfers', 'StockController@transfers');
        $this->router->post('/stock-transfers/store', 'StockController@storeTransfer');

        // Stock Adjustment
        $this->router->get('/stock-adjustments', 'StockController@adjustments');
        $this->router->post('/stock-adjustments/store', 'StockController@storeAdjustment');

        // Expenses
        $this->router->get('/expenses', 'ExpenseController@index');
        $this->router->get('/expenses/create', 'ExpenseController@create');
        $this->router->post('/expenses/store', 'ExpenseController@store');
        $this->router->get('/expenses/edit/{id}', 'ExpenseController@edit');
        $this->router->post('/expenses/update/{id}', 'ExpenseController@update');
        $this->router->post('/expenses/delete/{id}', 'ExpenseController@delete');

        // Income
        $this->router->get('/income', 'IncomeController@index');
        $this->router->get('/income/create', 'IncomeController@create');
        $this->router->post('/income/store', 'IncomeController@store');

        // Cash Book
        $this->router->get('/accounting/cash-book', 'CashBookController@index');
        $this->router->post('/accounting/cash-book/store', 'CashBookController@store');

        // Bank Book
        $this->router->get('/accounting/bank-book', 'BankBookController@index');
        $this->router->post('/accounting/bank-book/store', 'BankBookController@store');

        // Petty Cash
        $this->router->get('/accounting/petty-cash', 'PettyCashController@index');
        $this->router->post('/accounting/petty-cash/store', 'PettyCashController@store');
        $this->router->post('/accounting/petty-cash/approve/{id}', 'PettyCashController@approve');

        // Membership
        $this->router->get('/membership', 'MembershipController@index');
        $this->router->get('/membership/tiers', 'MembershipController@tiers');
        $this->router->post('/membership/tiers/store', 'MembershipController@storeTier');
        $this->router->get('/membership/assign', 'MembershipController@assign');
        $this->router->post('/membership/store', 'MembershipController@store');
        $this->router->post('/membership/cancel/{id}', 'MembershipController@cancel');

        // Loyalty
        $this->router->get('/loyalty', 'LoyaltyController@index');
        $this->router->get('/loyalty/rules', 'LoyaltyController@rules');
        $this->router->post('/loyalty/rules/store', 'LoyaltyController@storeRule');

        // Gift Cards
        $this->router->get('/gift-cards', 'GiftCardController@index');
        $this->router->get('/gift-cards/create', 'GiftCardController@create');
        $this->router->post('/gift-cards/store', 'GiftCardController@store');
        $this->router->post('/gift-cards/reload/{id}', 'GiftCardController@reload');

        // Marketing
        $this->router->get('/marketing', 'MarketingController@index');
        $this->router->get('/marketing/campaigns', 'MarketingController@campaigns');
        $this->router->post('/marketing/campaigns/store', 'MarketingController@storeCampaign');
        $this->router->get('/marketing/templates/email', 'MarketingController@emailTemplates');
        $this->router->post('/marketing/templates/email/store', 'MarketingController@storeEmailTemplate');
        $this->router->get('/marketing/templates/sms', 'MarketingController@smsTemplates');
        $this->router->post('/marketing/templates/sms/store', 'MarketingController@storeSmsTemplate');

        // Notifications
        $this->router->get('/notifications', 'NotificationController@index');
        $this->router->post('/notifications/read/{id}', 'NotificationController@markRead');
        $this->router->post('/notifications/read-all', 'NotificationController@markAllRead');

        // Reports
        $this->router->get('/reports', 'ReportController@index');
        $this->router->get('/reports/daily-sales', 'ReportController@dailySales');
        $this->router->get('/reports/monthly-sales', 'ReportController@monthlySales');
        $this->router->get('/reports/yearly-sales', 'ReportController@yearlySales');
        $this->router->get('/reports/profit-loss', 'ReportController@profitLoss');
        $this->router->get('/reports/inventory', 'ReportController@inventory');
        $this->router->get('/reports/employees', 'ReportController@employees');
        $this->router->get('/reports/customers', 'ReportController@customers');
        $this->router->get('/reports/appointments', 'ReportController@appointments');
        $this->router->get('/reports/cash-flow', 'ReportController@cashFlow');
        $this->router->get('/reports/expenses', 'ReportController@expenses');
        $this->router->get('/reports/purchases', 'ReportController@purchases');
        $this->router->get('/reports/stock-movement', 'ReportController@stockMovement');
        $this->router->get('/reports/top-services', 'ReportController@topServices');
        $this->router->get('/reports/top-employees', 'ReportController@topEmployees');
        $this->router->get('/reports/top-customers', 'ReportController@topCustomers');
        $this->router->get('/reports/export/{type}', 'ReportController@export');

        // Settings
        $this->router->get('/settings', 'SettingController@index');
        $this->router->post('/settings/update', 'SettingController@update');

        // Audit Logs
        $this->router->get('/audit-logs', 'AuditController@index');
        $this->router->get('/audit-logs/{id}', 'AuditController@show');

        // Activity Logs
        $this->router->get('/activity-logs', 'AuditController@activity');

        // Backup
        $this->router->get('/settings/backup', 'SettingController@backup');
        $this->router->post('/settings/backup/create', 'SettingController@createBackup');
        $this->router->post('/settings/backup/restore/{id}', 'SettingController@restoreBackup');
        $this->router->get('/settings/backup/download/{id}', 'SettingController@downloadBackup');
        $this->router->post('/settings/clear-data', 'SettingController@clearData');

        // AJAX routes
        $this->router->get('/api/dashboard/stats', 'DashboardController@stats');
        $this->router->get('/api/dashboard/revenue-chart', 'DashboardController@revenueChart');
        $this->router->get('/api/dashboard/appointments-today', 'DashboardController@appointmentsToday');

        // Dashboard AJAX routes (JS calls these)
        $this->router->get('/dashboard/revenue-chart', 'DashboardController@revenueChart');
        $this->router->get('/dashboard/top-services', 'DashboardController@topServices');
        $this->router->get('/dashboard/low-stock-data', 'DashboardController@lowStockData');
        $this->router->get('/dashboard/top-employees', 'DashboardController@topEmployees');
        $this->router->get('/api/customers/search', 'CustomerController@search');
        $this->router->get('/api/services/search', 'ServiceController@search');
        $this->router->get('/api/products/search', 'ProductController@search');
        $this->router->get('/api/employees/available', 'EmployeeController@available');

        // 404 fallback
        $this->router->any('/404', 'ErrorController@notFound');
        $this->router->any('/403', 'ErrorController@forbidden');
        $this->router->any('/500', 'ErrorController@serverError');
    }
}
