<?php

namespace Modules\CabBooking\Repositories\Api;

use Modules\CabBooking\Http\Resources\Riders\OnBoardingResource;
use Modules\CabBooking\Http\Resources\SettingsResource;
use Modules\CabBooking\Models\Onboarding;
use Modules\CabBooking\Models\CabBookingSetting;
use Prettus\Repository\Eloquent\BaseRepository;

class SettingRepository extends BaseRepository
{

    function model()
    {
        return CabBookingSetting::class;
    }

    public function index()
    {
        $settings = $this->model->latest('created_at')->first();
        $values = $settings['cabbooking_values'];
        $splashImage = getMedia($values['setting']['splash_screen_id'] ?? null);
        $driverSplash = getMedia($values['setting']['splash_driver_screen_id'] ?? null);
        $values['setting']['splash_screen_url'] = $splashImage?->original_url;
        $values['setting']['driver_splash_screen_url'] = $driverSplash?->original_url;

        $onBoardings = Onboarding::where('status', true)
            ->when(request()->filled('type'), function ($query) {
                $query->where('type', request()->input('type'));
            })?->get();

        $values['onboarding'] =  OnBoardingResource::collection($onBoardings ?? []);
        $settings['cabbooking_values'] = $values;
        return new SettingsResource($settings);

    }
}
