<?php

use App\Http\Controllers\Admin\AdminController;
use App\Http\Controllers\Auth\AuthController;
use App\Http\Controllers\Customer\CustomerController;
use App\Http\Controllers\CustomerApp\CustomerAppController;
use App\Http\Controllers\Partner\PartnerController;
use App\Http\Controllers\PartnerApp\PartnerAppController;
use App\Http\Controllers\PartnerApp\TeamController;
use App\Http\Controllers\Site\BookingController;
use App\Http\Controllers\SiteController;
use App\Http\Controllers\TeamMember\TeamMemberController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web routes
|--------------------------------------------------------------------------
*/

// Public site
Route::get('/', [SiteController::class, 'index'])->name('home');
Route::get('/services', [SiteController::class, 'services'])->name('services.index');
Route::get('/service/{service:slug}', [SiteController::class, 'service'])->name('services.show');
Route::get('/about', [SiteController::class, 'about'])->name('about');
Route::get('/contact', [SiteController::class, 'contact'])->name('contact');
Route::get('/become-partner', [SiteController::class, 'partner'])->name('become-partner');
Route::get('/help', [SiteController::class, 'help'])->name('help');
Route::get('/terms', [SiteController::class, 'terms'])->name('terms');
Route::get('/privacy', [SiteController::class, 'privacy'])->name('privacy');

// Auth
Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [AuthController::class, 'login'])->name('login.attempt');
    Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
    Route::post('/register', [AuthController::class, 'register'])->name('register.attempt');
});
Route::post('/logout', [AuthController::class, 'logout'])->middleware('auth')->name('logout');

// Booking flow
Route::get('/book/{service:slug}', [BookingController::class, 'show'])->name('book.show');
Route::get('/book/{service:slug}/summary', [BookingController::class, 'summary'])->name('book.summary');
Route::post('/book/{service:slug}', [BookingController::class, 'store'])->name('book.store');

// Track booking (public)
Route::get('/track', [BookingController::class, 'guestTrackForm'])->name('track.form');
Route::post('/track', [BookingController::class, 'track'])->name('track.attempt');
Route::get('/booking/{code}', [BookingController::class, 'publicView'])->name('booking.public');

// Customer App (mobile-first, app-style)
Route::prefix('customerapp')->name('app.')->group(function () {
    // Splash / public
    Route::get('/', [CustomerAppController::class, 'splash'])->name('splash');
    Route::get('/login', [CustomerAppController::class, 'showLogin'])->name('login');
    Route::post('/login', [CustomerAppController::class, 'login'])->name('login.attempt');
    Route::get('/register', [CustomerAppController::class, 'showRegister'])->name('register');
    Route::post('/register', [CustomerAppController::class, 'register'])->name('register.attempt');

    // Authenticated app pages
    Route::middleware(['auth', 'role:customer'])->group(function () {
        Route::get('/home', [CustomerAppController::class, 'home'])->name('home');
        Route::get('/services', [CustomerAppController::class, 'services'])->name('services');
        Route::get('/service/{service:slug}', [CustomerAppController::class, 'service'])->name('service');
        Route::get('/bookings', [CustomerAppController::class, 'bookings'])->name('bookings');
        Route::get('/booking/{code}', [CustomerAppController::class, 'booking'])->name('booking');
        Route::get('/schedule', [CustomerAppController::class, 'schedule'])->name('schedule');
        Route::post('/booking/{code}/cancel', [CustomerAppController::class, 'cancelSubscription'])->name('booking.cancel');
        Route::post('/booking/{code}/pause', [CustomerAppController::class, 'pauseSubscription'])->name('booking.pause');
        Route::post('/booking/{code}/resume', [CustomerAppController::class, 'resumeSubscription'])->name('booking.resume');
        Route::post('/booking/{code}/rate', [CustomerAppController::class, 'rate'])->name('booking.rate');
        Route::get('/profile', [CustomerAppController::class, 'profile'])->name('profile');
        Route::post('/profile', [CustomerAppController::class, 'profileUpdate'])->name('profile.update');
        Route::post('/location', [CustomerAppController::class, 'saveLocation'])->name('location.save');
        Route::post('/address', [CustomerAppController::class, 'addressSave'])->name('address.save');
        Route::delete('/address/{id}', [CustomerAppController::class, 'addressDestroy'])->name('address.destroy');
        Route::get('/help', [CustomerAppController::class, 'help'])->name('help');
        Route::get('/contact', [CustomerAppController::class, 'contact'])->name('contact');
        Route::get('/about', [CustomerAppController::class, 'about'])->name('about');
        Route::post('/logout', [CustomerAppController::class, 'logout'])->name('logout');
    });
});

// Customer
Route::middleware(['auth', 'role:customer'])->prefix('me')->name('customer.')->group(function () {
    Route::get('/bookings', [CustomerController::class, 'bookings'])->name('bookings');
    Route::get('/booking/{code}', [CustomerController::class, 'booking'])->name('booking');
    Route::post('/booking/{code}/cancel', [CustomerController::class, 'cancel'])->name('cancel');
    Route::post('/booking/{code}/pause', [CustomerController::class, 'pause'])->name('pause');
    Route::post('/booking/{code}/resume', [CustomerController::class, 'resume'])->name('resume');
    Route::post('/booking/{code}/cancel-subscription', [CustomerController::class, 'cancelSubscription'])->name('cancelSub');
    Route::post('/booking/{code}/rate', [CustomerController::class, 'rate'])->name('rate');
    Route::get('/profile', [CustomerController::class, 'profile'])->name('profile');
    Route::post('/profile', [CustomerController::class, 'profileUpdate'])->name('profile.update');
    Route::post('/address', [CustomerController::class, 'addressSave'])->name('address.save');
    Route::post('/profile/photo', [CustomerController::class, 'profilePhotoUpdate'])->name('profile.photo');
    Route::post('/profile/geo', [CustomerController::class, 'profileGeoUpdate'])->name('profile.geo');
    Route::get('/schedule', [CustomerController::class, 'schedule'])->name('schedule');
    Route::get('/services', [CustomerController::class, 'servicesBrowse'])->name('services.browse');
    Route::get('/services/{service:slug}', [CustomerController::class, 'serviceDetail'])->name('service.detail');
    Route::get('/track', [CustomerController::class, 'trackForm'])->name('track.form');
    Route::post('/track', [CustomerController::class, 'trackLookup'])->name('track.lookup');
});

// Team Member App (mobile-first, app-style)
Route::prefix('teammember')->name('tm.')->group(function () {
    Route::middleware(['auth', 'role:team_member'])->group(function () {
        Route::get('/', [TeamMemberController::class, 'dashboard'])->name('dashboard');
        Route::get('/visits', [TeamMemberController::class, 'visits'])->name('visits');
        Route::get('/visit/{visit}', [TeamMemberController::class, 'visit'])->name('visit');
        Route::post('/visit/{visit}/attendance', [TeamMemberController::class, 'visitMarkAttendance'])->name('visit.attendance');
        Route::get('/profile', [TeamMemberController::class, 'profile'])->name('profile');
        Route::get('/help', [TeamMemberController::class, 'help'])->name('help');
    });
});

// Partner App (mobile-first, app-style)
Route::prefix('partnerapp')->name('pa.')->group(function () {
    Route::get('/', [PartnerAppController::class, 'splash'])->name('splash');
    Route::get('/login', [PartnerAppController::class, 'showLogin'])->name('login');
    Route::post('/login', [PartnerAppController::class, 'login'])->name('login.attempt');
    Route::get('/register', [PartnerAppController::class, 'showRegister'])->name('register');
    Route::post('/register', [PartnerAppController::class, 'register'])->name('register.attempt');

    Route::middleware(['auth', 'role:partner'])->group(function () {
        Route::get('/home', [PartnerAppController::class, 'home'])->name('home');
        Route::get('/visits', [PartnerAppController::class, 'visits'])->name('visits');
        Route::get('/visit/{visit}', [PartnerAppController::class, 'visit'])->name('visit');
        Route::post('/visit/{visit}/accept', [PartnerAppController::class, 'visitAccept'])->name('visit.accept');
        Route::post('/visit/{visit}/reject', [PartnerAppController::class, 'visitReject'])->name('visit.reject');
        Route::post('/visit/{visit}/status', [PartnerAppController::class, 'visitUpdateStatus'])->name('visit.status');
        Route::post('/visit/{visit}/attendance', [PartnerAppController::class, 'visitMarkAttendance'])->name('visit.attendance');
        Route::post('/visit/{visit}/assign-team', [PartnerAppController::class, 'visitAssignTeam'])->name('visit.assignTeam');
        Route::delete('/visit/{visit}/assign-team', [PartnerAppController::class, 'visitUnassignTeam'])->name('visit.unassignTeam');

        // Partner self-service bookings
        Route::get('/bookings', [PartnerAppController::class, 'bookingsIndex'])->name('bookings');
        Route::get('/bookings/new', [PartnerAppController::class, 'bookingNew'])->name('bookings.new');
        Route::post('/bookings', [PartnerAppController::class, 'bookingCreate'])->name('bookings.create');
        Route::get('/bookings/{code}', [PartnerAppController::class, 'bookingShow'])->name('booking.show');

        // Monthly schedule (calendar view of all subscription visits)
        Route::get('/schedule', [PartnerAppController::class, 'schedule'])->name('schedule');

        // Leave management
        Route::get('/leaves', [PartnerAppController::class, 'leaves'])->name('leaves');
        Route::post('/leaves', [PartnerAppController::class, 'applyLeave'])->name('leave.apply');
        Route::post('/leaves/{leave}/cancel', [PartnerAppController::class, 'cancelLeave'])->name('leave.cancel');

        // Daily attendance with screenshot proof
        Route::get('/attendance', [PartnerAppController::class, 'attendance'])->name('attendance');
        Route::post('/attendance', [PartnerAppController::class, 'attendanceStore'])->name('attendance.store');

        Route::get('/earnings', [PartnerAppController::class, 'earnings'])->name('earnings');
        Route::get('/profile', [PartnerAppController::class, 'profile'])->name('profile');
        Route::post('/profile', [PartnerAppController::class, 'profileUpdate'])->name('profile.update');
        Route::post('/toggle-availability', [PartnerAppController::class, 'toggleAvailability'])->name('availability');
        Route::get('/help', [PartnerAppController::class, 'help'])->name('help');
        Route::get('/contact', [PartnerAppController::class, 'contact'])->name('contact');

        // Team members (partner self-service)
        Route::get('/team', [TeamController::class, 'index'])->name('team.index');
        Route::post('/team', [TeamController::class, 'store'])->name('team.store');
        Route::post('/team/{member}/toggle', [TeamController::class, 'toggle'])->name('team.toggle');
        Route::delete('/team/{member}', [TeamController::class, 'destroy'])->name('team.destroy');
        Route::get('/about', [PartnerAppController::class, 'about'])->name('about');
        Route::post('/logout', [PartnerAppController::class, 'logout'])->name('logout');
    });
});

// Partner
Route::middleware(['auth', 'role:partner'])->prefix('partner')->name('partner.')->group(function () {
    Route::get('/', [PartnerController::class, 'dashboard'])->name('dashboard');
    Route::get('/visit/{visit}', [PartnerController::class, 'visit'])->name('visit');
    Route::post('/visit/{visit}/accept', [PartnerController::class, 'visitAccept'])->name('visit.accept');
    Route::post('/visit/{visit}/reject', [PartnerController::class, 'visitReject'])->name('visit.reject');
    Route::post('/visit/{visit}/status', [PartnerController::class, 'visitUpdateStatus'])->name('visit.status');
    Route::post('/visit/{visit}/attendance', [PartnerController::class, 'visitMarkAttendance'])->name('visit.attendance');
    Route::post('/visit/{visit}/photo', [PartnerController::class, 'uploadPhoto'])->name('visit.photo');
    Route::post('/visit/{visit}/assign-team', [PartnerController::class, 'visitAssignTeam'])->name('visit.assignTeam');
    Route::delete('/visit/{visit}/assign-team', [PartnerController::class, 'visitUnassignTeam'])->name('visit.unassignTeam');
    // Legacy subscription-level routes
    Route::get('/subscription/{code}', [PartnerController::class, 'subscription'])->name('subscription');

    // Self-service — bookings, schedule, attendance, leaves, team
    Route::get('/bookings', [PartnerController::class, 'bookingsIndex'])->name('bookings');
    Route::get('/bookings/new', [PartnerController::class, 'bookingNew'])->name('bookings.new');
    Route::post('/bookings', [PartnerController::class, 'bookingCreate'])->name('bookings.create');
    Route::get('/bookings/{code}', [PartnerController::class, 'bookingShow'])->name('booking.show');

    Route::get('/schedule', [PartnerController::class, 'schedule'])->name('schedule');

    Route::get('/attendance', [PartnerController::class, 'attendance'])->name('attendance');
    Route::post('/attendance', [PartnerController::class, 'attendanceStore'])->name('attendance.store');

    Route::get('/leaves', [PartnerController::class, 'leaves'])->name('leaves');
    Route::post('/leaves', [PartnerController::class, 'applyLeave'])->name('leave.apply');
    Route::post('/leaves/{leave}/cancel', [PartnerController::class, 'cancelLeave'])->name('leave.cancel');

    Route::get('/team', [PartnerController::class, 'team'])->name('team');
    Route::post('/team', [PartnerController::class, 'teamStore'])->name('team.store');
    Route::post('/team/{member}/toggle', [PartnerController::class, 'teamToggle'])->name('team.toggle');
    Route::delete('/team/{member}', [PartnerController::class, 'teamDestroy'])->name('team.destroy');

    Route::get('/profile', [PartnerController::class, 'profile'])->name('profile');
    Route::post('/profile', [PartnerController::class, 'profileUpdate'])->name('profile.update');
    Route::post('/availability', [PartnerController::class, 'toggleAvailability'])->name('availability');
    Route::get('/earnings', [PartnerController::class, 'earnings'])->name('earnings');
});

// Admin
Route::middleware(['auth', \App\Http\Middleware\EnsureAdmin::class])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/', [AdminController::class, 'dashboard'])->name('dashboard');
    Route::get('/services', [AdminController::class, 'services'])->name('services.index');
    Route::get('/services/create', [AdminController::class, 'serviceCreate'])->name('services.create');
    Route::post('/services', [AdminController::class, 'serviceStore'])->name('services.store');
    Route::get('/services/{service}/edit', [AdminController::class, 'serviceEdit'])->name('services.edit');
    Route::put('/services/{service}', [AdminController::class, 'serviceUpdate'])->name('services.update');
    Route::delete('/services/{service}', [AdminController::class, 'serviceDestroy'])->name('services.destroy');

    Route::get('/categories', [AdminController::class, 'categories'])->name('categories');
    Route::post('/categories', [AdminController::class, 'categorySave'])->name('categories.save');
    Route::delete('/categories/{category}', [AdminController::class, 'categoryDestroy'])->name('categories.destroy');

    Route::get('/areas', [AdminController::class, 'areas'])->name('areas');
    Route::post('/areas', [AdminController::class, 'areaSave'])->name('areas.save');
    Route::delete('/areas/{area}', [AdminController::class, 'areaDestroy'])->name('areas.destroy');

    Route::get('/partners', [AdminController::class, 'partners'])->name('partners');
    Route::get('/partners/{user}', [AdminController::class, 'partnerShow'])->name('partners.show');
    Route::get('/partners/{user}/edit', [AdminController::class, 'partnerEdit'])->name('partners.edit');
    Route::put('/partners/{user}', [AdminController::class, 'partnerUpdate'])->name('partners.update');
    Route::post('/partners/{user}/approve', [AdminController::class, 'partnerApprove'])->name('partners.approve');
    Route::post('/partners/{user}/reject', [AdminController::class, 'partnerReject'])->name('partners.reject');
    Route::post('/partners/{user}/suspend', [AdminController::class, 'partnerSuspend'])->name('partners.suspend');
    Route::post('/partners/{user}/unsuspend', [AdminController::class, 'partnerUnsuspend'])->name('partners.unsuspend');
    Route::post('/partners/{user}/services', [AdminController::class, 'partnerAttachService'])->name('partners.services.attach');
    Route::delete('/partners/{user}/services/{service}', [AdminController::class, 'partnerDetachService'])->name('partners.services.detach');
    Route::post('/partners/{user}/areas', [AdminController::class, 'partnerAttachArea'])->name('partners.areas.attach');
    Route::delete('/partners/{user}/areas/{area}', [AdminController::class, 'partnerDetachArea'])->name('partners.areas.detach');
    Route::post('/partners/{user}/kyc', [AdminController::class, 'partnerUploadKyc'])->name('partners.kyc.upload');
    Route::get('/partners/{user}/team', [AdminController::class, 'partnerTeamIndex'])->name('partners.team.index');
    Route::post('/partners/{user}/team', [AdminController::class, 'partnerTeamStore'])->name('partners.team.store');
    Route::post('/partners/{user}/team/{member}/toggle', [AdminController::class, 'partnerTeamToggle'])->name('partners.team.toggle');
    Route::delete('/partners/{user}/team/{member}', [AdminController::class, 'partnerTeamDestroy'])->name('partners.team.destroy');
    Route::delete('/partners/{user}', [AdminController::class, 'partnerDestroy'])->name('partners.destroy');

    Route::get('/customers', [AdminController::class, 'customers'])->name('customers');

    Route::get('/bookings', [AdminController::class, 'bookings'])->name('bookings');
    Route::get('/bookings/{booking}', [AdminController::class, 'bookingShow'])->name('bookings.show');
    Route::post('/bookings/{booking}/assign', [AdminController::class, 'bookingAssign'])->name('bookings.assign');
    Route::post('/bookings/{booking}/cancel', [AdminController::class, 'bookingCancel'])->name('bookings.cancel');
    Route::post('/bookings/{booking}/visits', [AdminController::class, 'bookingGenerateVisit'])->name('bookings.visits.create');
    Route::patch('/bookings/{booking}/visits/{visit}', [AdminController::class, 'bookingRescheduleVisit'])->name('bookings.visits.update');
    Route::delete('/bookings/{booking}/visits/{visit}', [AdminController::class, 'bookingCancelVisit'])->name('bookings.visits.destroy');

    Route::get('/ratings', [AdminController::class, 'ratings'])->name('ratings');
    Route::get('/settings', [AdminController::class, 'settings'])->name('settings');
    Route::post('/settings', [AdminController::class, 'settingsSave'])->name('settings.save');

    // Partner operations
    Route::get('/leaves', [AdminController::class, 'leaves'])->name('leaves');
    Route::post('/leaves/{leave}/approve', [AdminController::class, 'leaveApprove'])->name('leaves.approve');
    Route::post('/leaves/{leave}/reject', [AdminController::class, 'leaveReject'])->name('leaves.reject');
    Route::get('/attendances', [AdminController::class, 'attendances'])->name('attendances');
});
