<?php

namespace App\Http\Controllers\Admin;

use App\CentralLogics\Helpers;
use App\Exceptions\CourierException;
use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Services\Courier\CourierBookingService;
use App\Services\Courier\CustomerCourierPerformanceService;
use App\Services\Courier\CourierTrackingService;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;

class OrderCourierController extends Controller
{
    public function __construct(
        private readonly CourierBookingService $courierBookingService,
        private readonly CourierTrackingService $courierTrackingService
    ) {
    }

    public function send(Order $order)
    {
        if (! Helpers::module_permission_check('order')) {
            Toastr::error(translate('messages.access_denied'));
            return back();
        }

        try {
            $this->courierBookingService->sendToCourier($order, auth('admin')->id());
            Toastr::success('Order has been sent to courier successfully.');
        } catch (CourierException $exception) {
            Toastr::error($exception->getMessage());
        } catch (\Throwable $throwable) {
            info($throwable->getMessage());
            Toastr::error('Courier booking failed. Please try again.');
        }

        return back();
    }

    public function sync(Order $order)
    {
        if (! Helpers::module_permission_check('order')) {
            Toastr::error(translate('messages.access_denied'));
            return back();
        }

        try {
            $this->courierTrackingService->sync($order, auth('admin')->id());
            Toastr::success('Courier status synced successfully.');
        } catch (CourierException $exception) {
            Toastr::error($exception->getMessage());
        } catch (\Throwable $throwable) {
            info($throwable->getMessage());
            Toastr::error('Courier sync failed. Please try again.');
        }

        return back();
    }

    public function manualAssign(Order $order, Request $request)
    {
        if (! Helpers::module_permission_check('order')) {
            Toastr::error(translate('messages.access_denied'));
            return back();
        }

        if (! Schema::hasColumn('orders', 'delivery_service_name')) {
            Toastr::error('Manual courier assignment is not available yet. Please run database migrations.');
            return back();
        }

        $allowed = array_keys(app(CustomerCourierPerformanceService::class)->supportedCouriers());
        $allowed = array_diff($allowed, ['inhouse']);

        $courier = $request->input('delivery_service_name');

        if ($courier !== null && $courier !== '' && ! in_array($courier, $allowed, true)) {
            Toastr::error('Invalid courier selected.');
            return back();
        }

        $order->delivery_service_name = ($courier === '' || $courier === null) ? null : $courier;
        $order->save();

        if ($order->delivery_service_name) {
            Toastr::success('Courier assigned: ' . ucfirst($order->delivery_service_name));
        } else {
            Toastr::success('Manual courier assignment cleared.');
        }

        return back();
    }
}
