<?php

namespace App\Http\Controllers\Admin;

use Exception;
use App\Models\Order;
use App\Models\OrderHistory;
use App\Models\ThemeSetting;
use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade\Pdf;
use App\Services\CompanyService;
use App\Exports\TodayOrderExport;
use App\Services\TodayOrderService;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Requests\PaginateRequest;
use App\Http\Requests\OrderStatusRequest;
use Dipokhalder\Settings\Facades\Settings;
use Illuminate\Routing\Controllers\Middleware;
use Illuminate\Routing\Controllers\HasMiddleware;
use App\Http\Resources\TodayTomorrowSimpleOrderResource;
use App\Http\Resources\TodayTomorrowOrderDetailsResource;

class TodayOrderController extends AdminController implements HasMiddleware
{
    private TodayOrderService $todayOrderService;
    private CompanyService $companyService;

    public function __construct(TodayOrderService $todayOrderService, CompanyService $companyService)
    {
        parent::__construct();
        $this->todayOrderService = $todayOrderService;
        $this->companyService = $companyService;
    }

    public static function middleware(): array
    {
        return [
            new Middleware('permission:all-orders', only: ['index', 'show', 'export', 'pdf', 'changeStatus'])
        ];
    }

    public function index(PaginateRequest $request): \Illuminate\Http\Response | \Illuminate\Http\Resources\Json\AnonymousResourceCollection | \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\Routing\ResponseFactory
    {
        try {
            return TodayTomorrowSimpleOrderResource::collection($this->todayOrderService->list($request));
        } catch (Exception $exception) {
            return response(['status' => false, 'message' => $exception->getMessage()], 422);
        }
    }

    public function show(OrderHistory $orderHistory, Request $request): \Illuminate\Http\Response | TodayTomorrowOrderDetailsResource | \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\Routing\ResponseFactory
    {
        try {
            return new TodayTomorrowOrderDetailsResource($this->todayOrderService->show($orderHistory, $request, false));
        } catch (Exception $exception) {
            return response(['status' => false, 'message' => $exception->getMessage()], 422);
        }
    }

    public function export(PaginateRequest $request): \Illuminate\Http\Response | \Symfony\Component\HttpFoundation\BinaryFileResponse | \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\Routing\ResponseFactory
    {
        try {
            return Excel::download(new TodayOrderExport($this->todayOrderService, $request), 'Today-Order.xlsx');
        } catch (Exception $exception) {
            return response(['status' => false, 'message' => $exception->getMessage()], 422);
        }
    }
    public function pdf(PaginateRequest $request): mixed
    {
        try {
            $company = $this->companyService->list();
            $theme_logo   = ThemeSetting::where(['key' => 'theme_logo'])->first()?->logo;
            $copyright   = Settings::group('site')->get('site_copyright');
            $orders = $this->todayOrderService->list($request);

            $pdf = Pdf::loadView('pdf.today_orders', compact('company', 'theme_logo', 'orders', 'copyright'))
                ->setPaper('a4');
            return response()->stream(
                fn() => print($pdf->output()),
                200,
                [
                    'Content-Type' => 'application/pdf',
                    'Content-Disposition' => 'attachment; filename="today_order_report.pdf"',
                ]
            );
        } catch (Exception $exception) {
            return response(['status' => false, 'message' => $exception->getMessage()], 422);
        }
    }

    public function changeStatus(OrderStatusRequest $request, OrderHistory $orderHistory): \Illuminate\Http\Response | TodayTomorrowOrderDetailsResource | \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\Routing\ResponseFactory
    {
        try {
            return new TodayTomorrowOrderDetailsResource($this->todayOrderService->changeStatus($request, $orderHistory));
        } catch (Exception $exception) {
            return response(['status' => false, 'message' => $exception->getMessage()], 422);
        }
    }

    public function selectDeliveryBoy(Request $request, OrderHistory $orderHistory): \Illuminate\Http\Response | TodayTomorrowOrderDetailsResource | \Illuminate\Contracts\Foundation\Application | \Illuminate\Contracts\Routing\ResponseFactory
    {
        try {
            return new TodayTomorrowOrderDetailsResource($this->todayOrderService->selectDeliveryBoy($request, $orderHistory, false));
        } catch (Exception $exception) {
            return response(['status' => false, 'message' => $exception->getMessage()], 422);
        }
    }
}