<?php

namespace App\Modules\Trip\Services;

use App\Models\Trip;
use App\Http\Resources\TripResource;
use App\Support\Services\BaseService;

use App\Modules\Trip\DTOs\StoreTripDTO;
use Illuminate\Database\Eloquent\Builder;
use App\Support\Services\Localization\MultilingualText;

class TripService extends BaseService
{
    public function __construct(
        protected MultilingualText $multilingualText,
    ) {}

    public function create($user, StoreTripDTO $dto)
    {
        if (!$user->is_traveler) {
            return $this->error(
                'Only travelers can create trips',
                403
            );
        }

        $outbound = $this->multilingualText->generate(
            $dto->outbound
        );

        $inbound = $this->multilingualText->generate(
            $dto->inbound
        );

        $description = $this->multilingualText->generate(
            $dto->description
        );

        $trip = Trip::create([

            'user_id' => $user->id,

            'outbound_ar' => $outbound['ar'],
            'outbound_en' => $outbound['en'],
            'outbound_zh' => $outbound['zh'],

            'inbound_ar' => $inbound['ar'],
            'inbound_en' => $inbound['en'],
            'inbound_zh' => $inbound['zh'],

            'description_ar' => $description['ar'],
            'description_en' => $description['en'],
            'description_zh' => $description['zh'],

            'departure_at' => $dto->departure_at,

            'is_active' => true,
        ]);

        return $this->success([
            'trip' => new TripResource($trip),
        ], 'Trip created successfully', 201);
    }

    public function show($user, $id)
    {
        $trip = Trip::where('id', $id)
            ->where('user_id', $user->id)
            ->first();

        if (!$trip) {
            return $this->notFound('Trip not found');
        }

        return $this->success([
            'trip' => new TripResource($trip),
        ]);
    }

    public function list($user)
    {
        $trips = Trip::where('user_id', $user->id)
            ->latest()
            ->paginate(10);

        return $this->success([
            'trips' => TripResource::collection($trips),
            'pagination' => [
                'current_page' => $trips->currentPage(),
                'last_page' => $trips->lastPage(),
                'per_page' => $trips->perPage(),
                'total' => $trips->total(),
            ]
        ]);
    }

    public function delete($user, $id)
    {
        $trip = Trip::where('id', $id)
            ->where('user_id', $user->id)
            ->first();

        if (!$trip) {
            return $this->notFound('Trip not found');
        }

        $trip->delete();

        return $this->success([], 'Trip deleted');
    }

    public function update($user, $id, $dto)
    {
        $trip = Trip::where('id', $id)
            ->where('user_id', $user->id)
            ->first();

        if (!$trip) {
            return $this->notFound('Trip not found');
        }

        $data = [];

        /*
        |--------------------------------------------------------------------------
        | Outbound
        |--------------------------------------------------------------------------
        */

        if (!is_null($dto->outbound)) {

            $outbound = $this->multilingualText->generate(
                $dto->outbound
            );

            $data['outbound_ar'] = $outbound['ar'];
            $data['outbound_en'] = $outbound['en'];
            $data['outbound_zh'] = $outbound['zh'];
        }

        /*
        |--------------------------------------------------------------------------
        | Inbound
        |--------------------------------------------------------------------------
        */

        if (!is_null($dto->inbound)) {

            

            $inbound = $this->multilingualText->generate(
                $dto->inbound
            );

            $data['inbound_ar'] = $inbound['ar'];
            $data['inbound_en'] = $inbound['en'];
            $data['inbound_zh'] = $inbound['zh'];
        }

        /*
        |--------------------------------------------------------------------------
        |  description
        |--------------------------------------------------------------------------
        */
        if (!is_null($dto->description)) {

            $description = $this->multilingualText->generate(
                $dto->description
            );

            $data['description_ar'] = $description['ar'];
            $data['description_en'] = $description['en'];
            $data['description_zh'] = $description['zh'];
        }
        /*
        |--------------------------------------------------------------------------
        | Other Fields
        |--------------------------------------------------------------------------
        */

        if (!is_null($dto->departure_at)) {
            $data['departure_at'] = $dto->departure_at;
        }

        if (!is_null($dto->is_active)) {
            $data['is_active'] = $dto->is_active;
        }

        $trip->update($data);

        return $this->success([
            'trip' => new TripResource(
                $trip->fresh()
            ),
        ], 'Trip updated successfully');
    }

    public function search($dto)
    {
        $locale = app()->getLocale();

        $outboundColumn = match ($locale) {
            'ar' => 'outbound_ar',
            'zh' => 'outbound_zh',
            default => 'outbound_en',
        };

        $inboundColumn = match ($locale) {
            'ar' => 'inbound_ar',
            'zh' => 'inbound_zh',
            default => 'inbound_en',
        };

        // $query = Trip::query()->where('is_active', true);

        $query = Trip::query()->with('user')->where('is_active', true);

        /*
        |--------------------------------------------------------------------------
        | From
        |--------------------------------------------------------------------------
        */

        if ($dto->from) {

            $query->where(
                $outboundColumn,
                'LIKE',
                '%' . $dto->from . '%'
            );
        }

        /*
        |--------------------------------------------------------------------------
        | To
        |--------------------------------------------------------------------------
        */

        if ($dto->to) {

            $query->where(
                $inboundColumn,
                'LIKE',
                '%' . $dto->to . '%'
            );
        }

        /*
        |--------------------------------------------------------------------------
        | Travel Date
        |--------------------------------------------------------------------------
        */

        if ($dto->departure_date) {

            $query->whereDate(
                'departure_at',
                $dto->departure_date
            );
        }

        $trips = $query
            ->latest()
            ->paginate(10);

        return $this->success([

            'trips' => TripResource::collection($trips),

            'pagination' => [

                'current_page' => $trips->currentPage(),
                'last_page' => $trips->lastPage(),
                'per_page' => $trips->perPage(),
                'total' => $trips->total(),
            ]

        ]);
    }

}