<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\LanguageController;
use App\Http\Controllers\CRM\IdCardController;
use App\Http\Controllers\Admin\StuffController;
use App\Http\Controllers\CRM\AddressController;
use App\Http\Controllers\CRM\CompanyController;
use App\Http\Controllers\CRM\PassportController;
use App\Http\Controllers\CRM\IndividualController;
use App\Http\Controllers\CRM\MoneyAccountController;
use App\Http\Controllers\Backend\DashboardController;
use App\Http\Controllers\INSURANCE\ContactController;
use App\Http\Controllers\INSURANCE\InsurerController;
use App\Http\Controllers\CRM\CompanyContactController;
use App\Http\Controllers\INSURANCE\InsuranceTypeController;
use App\Http\Controllers\INSURANCE\InsurancePolicyController;
use App\Http\Controllers\INSURANCE\CommissionDefaultController;
use App\Http\Controllers\INSURANCE\InsurancePolicyDocsReturnedController;



Route::get('/', function () {
    return view('welcome');
});


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

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::get('/profile-show', [UserController::class, 'show'])->name('profile.show');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

Route::group(['prefix' => '_cp', 'middleware' => 'auth'], function () {
    Route::group(['middleware' => 'role:stuff'], function () {
        Route::controller(StuffController::class)->group(function () {
            Route::get('/', 'index')->name('stuff.dashboard');

            // Users DataTable Routes
            Route::get('/users', [UserController::class, 'index'])->name('users.index');       // page with DataTable
            Route::get('/users/data', [UserController::class, 'data'])->name('users.data');    // AJAX data endpoint
            Route::get('/users/export', [UserController::class, 'export'])->name('users.export');  // Export XML
            Route::delete('/users/{user}', [UserController::class, 'destroy'])->name('users.destroy');  // Delete single
            Route::post('/users/bulk-delete', [UserController::class, 'destroyMultiple'])->name('users.bulk-delete');  // Bulk delete     

            Route::get('/individuals/data', [IndividualController::class, 'data'])->name('individuals.data');


            Route::get('/companies/data', [CompanyController::class, 'data'])->name('companies.data');
        });

        // CRM Dashboards

        Route::get('/crm-dashboard', [DashboardController::class, 'crm'])->name('crm.dashboard');
        Route::get('/osiguranje-dashboard', [DashboardController::class, 'osiguranje'])->name('osiguranje.dashboard');

        Route::resource('individuals', IndividualController::class, ['as' => 'crm']);
        Route::post('individuals/{individual}/idcards', [IdCardController::class, 'store'])->name('crm.idcards.store');
        Route::put('individuals/{individual}/idcards', [IdCardController::class, 'update'])->name('crm.idcards.update');
        Route::post('individuals/{individual}/passports', [PassportController::class, 'store'])->name('crm.passports.store');
        Route::put('individuals/{individual}/passports', [PassportController::class, 'update'])->name('crm.passports.update');


        // For example, if you want separate routes for Residence vs. Mailing
        Route::post('individuals/{individual}/addresses/residence', [AddressController::class, 'storeResidence'])
            ->name('crm.individuals.addresses.storeResidence');

        Route::post('individuals/{individual}/addresses/mailing', [AddressController::class, 'storeMailing'])
            ->name('crm.individuals.addresses.storeMailing');

        // If you want a single update route for any address
        Route::put('individuals/{individual}/addresses/{address}', [AddressController::class, 'update'])
            ->name('crm.individuals.addresses.update');


        // CRM Company

        // For example, if you want separate routes for Residence vs. Mailing
        Route::post('companies/{company}/addresses/residence', [AddressController::class, 'storeResidenceComapny'])
            ->name('crm.companies.addresses.storeResidence');

        // If you want a single update route for any address
        Route::put('companies/{company}/addresses/{address}', [AddressController::class, 'updateAddressComapny'])
            ->name('crm.companies.addresses.updateResidence');



        Route::resource('insurers', InsurerController::class, ['as' => 'osiguranje']);

        // For example, if you want separate routes for Residence vs. Mailing
        Route::post('insurers/{insurer}/addresses/residence', [AddressController::class, 'storeResidenceOsiguranje'])
            ->name('osiguranje.insurers.addresses.storeResidence');

        // If you want a single update route for any address
        Route::put('insurers/{insurer}/addresses/{address}', [AddressController::class, 'updateOsiguranje'])
            ->name('osiguranje.insurers.addresses.update');


        // Insurance Types and Commissons
        Route::get('/insurance-types', [InsuranceTypeController::class, 'index'])->name('insurance-types.index');



        Route::prefix('insurers/{insurer}/commission-defaults')->name('osiguranje.commission-defaults.')->group(function () {
            Route::get('/edit', [CommissionDefaultController::class, 'edit'])->name('edit');
            Route::put('/', [CommissionDefaultController::class, 'update'])->name('update');

            // For example, if you want a create form:
            Route::get('/create', [CommissionDefaultController::class, 'create'])->name('create');
            Route::post('/', [CommissionDefaultController::class, 'store'])->name('store');
        });
        
        Route::resource('insurers.contacts', ContactController::class);

        Route::prefix('entities/{entity}/insurance-policies')->scopeBindings()->name('osiguranje.insurance-policies.')->group(function () {
            Route::get('/', [InsurancePolicyController::class, 'index'])->name('index');
            Route::get('/create', [InsurancePolicyController::class, 'create'])->name('create');
            Route::post('/store', [InsurancePolicyController::class, 'store'])->name('store');
            Route::get('/{insurancePolicy}/edit', [InsurancePolicyController::class, 'edit'])->name('edit');
            Route::put('/{insurancePolicy}', [InsurancePolicyController::class, 'update'])->name('update');
            Route::delete('/{insurancePolicy}', [InsurancePolicyController::class, 'destroy'])->name('destroy');

            Route::patch('/{insurancePolicy}/docs-returned',InsurancePolicyDocsReturnedController::class)->name('docs-returned');
        });




        Route::get('/insurers/{insurer}/commission-defaults', [CommissionDefaultController::class, 'index'])
            ->name('insurers.commission-defaults');


        Route::resource('companies', CompanyController::class, ['as' => 'crm']);

        Route::prefix('companies/{company}')->name('crm.companies.')->group(function () {
            Route::post('contacts', [CompanyContactController::class, 'store'])->name('contacts.store');
            Route::patch('contacts/{contact}', [CompanyContactController::class, 'update'])->name('contacts.update');
            Route::delete('contacts/{contact}', [CompanyContactController::class, 'destroy'])->name('contacts.destroy');
        });
        

        Route::post('individuals/accounts/{individual}', [MoneyAccountController::class, 'store'])->name('individual.accounts.store');
        Route::put('individuals/accounts/{individual}', [MoneyAccountController::class, 'store'])->name('individual.accounts.update');
    });
});

Route::middleware(['web'])->group(function () {
    Route::get('/set-locale/{locale?}', [LanguageController::class, 'setLocale'])->name('set.locale');
});

require __DIR__ . '/auth.php';
