<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\ForgotPasswordController;
use App\Http\Controllers\Api\BookingController;
use App\Http\Controllers\Api\RatingController;
use App\Http\Controllers\Api\TailorDashboardController;
use App\Http\Controllers\Api\TailorCalendarController;
use App\Http\Controllers\Api\TailorProfileController;
use App\Http\Controllers\Api\TailorServiceController;
use App\Http\Controllers\Api\TailorSearchController;
use App\Http\Controllers\Api\TailorSpecializationController;
use App\Http\Controllers\Api\TailorGalleryController;
use App\Http\Controllers\Api\TailorController;
use App\Http\Controllers\Api\AdminDashboardController;
use App\Http\Controllers\Api\CustomerController;
use App\Http\Controllers\Api\EmailVerificationController;
use App\Http\Controllers\Api\MidtransController;
use App\Http\Controllers\Api\WalletController;
use App\Http\Controllers\Api\AdminWithdrawalController;
use App\Http\Controllers\Api\DocumentationController;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
|
*/

Route::middleware('api')->group(function () {
    // Test route
    Route::get('/test', function () {
        return response()->json([
            'message' => 'API is working'
        ]);
    });

    // Email verification routes
    Route::get('/email/verify/{id}/{hash}', [EmailVerificationController::class, 'verify'])
        ->middleware('signed')
        ->name('verification.verify');

    // Get all specializations (public)
    Route::get('/specializations', [AuthController::class, 'getSpecializations']);
    Route::get('/specializations/all', [TailorSpecializationController::class, 'getAllSpecializations']);

    // Search tailors
    Route::get('/tailors/search', [TailorSearchController::class, 'searchBySpecialization']);
    Route::get('/tailors/search/name/{name}', [TailorSearchController::class, 'searchByName']);
    
    // Protected routes
    Route::middleware('auth:sanctum')->group(function () {
        // Other email verification routes
        Route::post('/email/verification-notification', [EmailVerificationController::class, 'sendVerificationEmail']);
        Route::post('/email/verification-resend', [EmailVerificationController::class, 'resend']);

        Route::post('/logout', [AuthController::class, 'logout']);
        Route::get('/profile', [AuthController::class, 'profile']);
        Route::post('/profile', [AuthController::class, 'updateProfile']);

        // Get recommended tailors for customer
        Route::get('/tailors/recommended', [TailorSearchController::class, 'getRecommended']);

        // Booking routes for customers
        Route::middleware(['can:pelanggan'])->prefix('bookings')->group(function () {
            Route::post('/', [BookingController::class, 'store']);
            Route::get('/customer', [BookingController::class, 'customerBookings']);
            Route::get('/customer/status/{status}', [BookingController::class, 'customerBookingsByStatus']);
            Route::get('/{booking}', [BookingController::class, 'show']);
            Route::post('/{booking}/cancel', [BookingController::class, 'cancelBooking']);
        });
        
        // Direct booking from tailor detail
        Route::post('/tailors/{tailor}/book', [BookingController::class, 'bookTailor'])->middleware('can:pelanggan');

        // Booking routes for tailors
        Route::middleware(['can:penjahit'])->prefix('bookings')->group(function () {
            Route::post('/{booking}/accept', [BookingController::class, 'acceptBooking']);
            Route::post('/{booking}/reject', [BookingController::class, 'rejectBooking']);
            Route::patch('/{booking}/status', [BookingController::class, 'updateStatus']);
            Route::post('/{booking}/measurements', [BookingController::class, 'updateMeasurements']);
            Route::post('/{booking}/repair', [BookingController::class, 'updateRepairDetails']);
            Route::post('/{booking}/complete', [BookingController::class, 'completeBooking']);
            Route::patch('/{booking}/price', [BookingController::class, 'updatePrice']);
            Route::post('/{booking}/payment', [BookingController::class, 'updatePaymentStatus']);
            Route::post('/{booking}/completion-payment', [BookingController::class, 'processCompletionPayment']);
            Route::get('/{booking}', [BookingController::class, 'show']);
        });

        // Penjahit protected routes
        Route::middleware(['can:penjahit'])->prefix('penjahit')->group(function () {
            Route::get('/dashboard', [TailorDashboardController::class, 'index']);
            Route::post('/specializations', [AuthController::class, 'updateSpecializations']);
            
            // Calendar routes
            Route::get('/calendar/{month}/{year}', [TailorCalendarController::class, 'getCalendarBookings']);
            Route::get('/calendar/date/{date}', [TailorCalendarController::class, 'getDateBookings']);
            
            // Shop profile routes
            Route::get('/shop-profile', [TailorProfileController::class, 'getProfile']);
            Route::post('/shop-profile', [TailorProfileController::class, 'updateProfile']);
            Route::delete('/shop-profile/photo', [TailorProfileController::class, 'deleteProfilePhoto']);

            // Gallery routes
            Route::get('/gallery', [TailorGalleryController::class, 'index']);
            Route::post('/gallery', [TailorGalleryController::class, 'store']);
            Route::put('/gallery/{gallery}', [TailorGalleryController::class, 'update']);
            Route::delete('/gallery/{gallery}', [TailorGalleryController::class, 'destroy']);

            // Services routes
            Route::get('/services', [TailorServiceController::class, 'index']);
            Route::post('/services', [TailorServiceController::class, 'store']);
            Route::put('/services/{service}', [TailorServiceController::class, 'update']);
            Route::delete('/services/{service}', [TailorServiceController::class, 'destroy']);
            Route::patch('/services/{service}/toggle', [TailorServiceController::class, 'toggleAvailability']);
            
            // Booking routes for tailors
            Route::get('/bookings', [BookingController::class, 'tailorBookings']);
            Route::get('/bookings/status/{status}', [BookingController::class, 'tailorBookingsByStatus']);
        });

        // Public routes for viewing tailor services and gallery
        Route::get('/tailors/{tailor}/services', [TailorServiceController::class, 'getTailorServices']);
        Route::get('/tailors/{tailor}/gallery', [TailorGalleryController::class, 'getTailorGallery']);

        // Rating routes
        Route::post('bookings/{booking}/rate', [RatingController::class, 'store']);
        Route::put('ratings/{rating}', [RatingController::class, 'update']);

        // Upload foto profil
        Route::post('/profile/photo', [AuthController::class, 'uploadProfilePhoto']);

        // Get completed booking details
        Route::get('bookings/{booking}/completed-details', [BookingController::class, 'getCompletedBookingDetails']);
        
        // Accept booking and change status to diproses
        Route::post('bookings/{booking}/accept', [BookingController::class, 'acceptBooking']);

        // Complete booking
        Route::post('bookings/{booking}/complete', [BookingController::class, 'completeBooking']);

        // Midtrans Payment routes
        Route::post('/bookings/{booking}/midtrans/pay', [MidtransController::class, 'initiatePayment']);
        Route::get('/bookings/{booking}/payment-status', [MidtransController::class, 'checkPaymentStatus']);
        Route::post('/bookings/{booking}/payment-status/check', [MidtransController::class, 'manualCheckStatus']);

        // Wallet routes
        Route::get('/wallet', [WalletController::class, 'getWallet']);
        Route::get('/bank-accounts', [WalletController::class, 'getBankAccounts']);
        Route::post('/bank-accounts', [WalletController::class, 'registerBankAccount']);
        Route::post('/withdrawals', [WalletController::class, 'requestWithdrawal']);
        Route::get('/withdrawals', [WalletController::class, 'getWithdrawalHistory']);
    });

    // Get tailor detail
    Route::get('/tailors/{id}', [TailorSearchController::class, 'getTailorDetail'])->where('id', '[0-9]+');

    // Pelanggan routes
    Route::prefix('pelanggan')->group(function () {
        Route::post('/register', [AuthController::class, 'registerPelanggan']);
        Route::post('/login', [AuthController::class, 'loginPelanggan']);
        Route::post('/forgot-password', [ForgotPasswordController::class, 'sendResetLinkEmail']);
        Route::post('/reset-password', [ForgotPasswordController::class, 'reset']);
    });

    // Penjahit routes
    Route::prefix('penjahit')->group(function () {
        Route::post('/register', [AuthController::class, 'registerPenjahit']);
        Route::post('/login', [AuthController::class, 'loginPenjahit']);
        Route::post('/forgot-password', [ForgotPasswordController::class, 'sendResetLinkEmail']);
        Route::post('/reset-password', [ForgotPasswordController::class, 'reset']);
    });

    Route::get('tailors/{tailor}/ratings', [RatingController::class, 'getTailorRatings']);

    // Customer dashboard
    Route::get('/dashboard', [TailorSearchController::class, 'getDashboardData'])->middleware('auth:sanctum', 'role:pelanggan');

    // Admin routes
    Route::post('/admin/login', [AuthController::class, 'loginAdmin']);
    
    // Admin protected routes
    Route::middleware(['auth:sanctum', 'can:admin'])->group(function () {
        Route::get('/admin/dashboard', [AdminDashboardController::class, 'index']);
        
        // Admin profile routes
        Route::get('/admin/profile', [AuthController::class, 'profile']);
        Route::post('/admin/profile', [AuthController::class, 'updateProfile']);
        Route::post('/admin/profile/photo', [AuthController::class, 'uploadProfilePhoto']);
        
        // Customer management routes
        Route::get('/customers', [CustomerController::class, 'getAllCustomers']);
        Route::get('/customers/{id}', [CustomerController::class, 'getCustomerDetail']);
        Route::get('/customers/{id}/transactions', [CustomerController::class, 'getCustomerTransactions']);
        Route::put('/customers/{id}/specializations', [CustomerController::class, 'updateCustomerSpecializations']);
        Route::delete('/customers/{id}', [CustomerController::class, 'deleteCustomer']);

        // Specialization management routes
        Route::get('/specializations', [TailorSpecializationController::class, 'index']);
        Route::post('/specializations', [TailorSpecializationController::class, 'store']);
        Route::get('/specializations/{id}', [TailorSpecializationController::class, 'show']);
        Route::put('/specializations/{id}', [TailorSpecializationController::class, 'update']);
        Route::delete('/specializations/{id}', [TailorSpecializationController::class, 'destroy']);
        Route::post('specializations/{id}/update', [TailorSpecializationController::class, 'update']);

        // Booking management routes
        Route::get('/bookings', [BookingController::class, 'getAllBookings']);

        // Admin withdrawal routes
        Route::get('/admin/withdrawals/pending', [AdminWithdrawalController::class, 'getPendingWithdrawals']);
        Route::get('/admin/bank-accounts/pending', [AdminWithdrawalController::class, 'getPendingBankAccounts']);
        Route::post('/admin/withdrawals/{withdrawal}/process', [AdminWithdrawalController::class, 'processWithdrawal']);
        Route::post('/admin/bank-accounts/{bankAccount}/verify', [AdminWithdrawalController::class, 'verifyBankAccount']);
        Route::get('/admin/bank-accounts/verified', [AdminWithdrawalController::class, 'getVerifiedBankAccounts']);
        Route::get('/admin/bank-accounts/{bankAccount}', [AdminWithdrawalController::class, 'getBankAccountDetail']);
        Route::get('/admin/withdrawals/history', [AdminWithdrawalController::class, 'getWithdrawalHistory']);
    });
    
    // Get all tailors with complete data
    Route::get('/tailors', [TailorController::class, 'getAllTailors']);

    // Get all tailors without rating filter (for admin only)
    Route::middleware(['auth:sanctum', 'can:admin'])->group(function () {
        Route::get('/tailors/all', [TailorController::class, 'getAllTailorsNoFilter']);
        Route::delete('/tailors/{id}', [TailorController::class, 'deleteTailor']);
        Route::put('/tailors/{id}', [TailorController::class, 'updateTailor']);
    });

    // Midtrans notification callback - no auth required
    Route::post('/midtrans/notification', [MidtransController::class, 'notificationHandler']);

    // Documentation routes
    Route::get('/doc', [DocumentationController::class, 'index']);
    Route::get('/doc/{section}', [DocumentationController::class, 'show']);
});