<?php

namespace App\Modules\applications\Logic;

use A7kz\Platform\Models\UniModel;
use A7kz\Platform\Modules\Platform\Acl\Facades\Acl;
use A7kz\Platform\Modules\Platform\Core\Services\Base\Logic;
use App\Models\User;
use App\Modules\applications\Enum\ApplicationStatus;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;

class Cancel extends Logic
{
    public function run()
    {
        if (Carbon::now() > $this->row->ended_at) {
            $this->message = 'Вы не можете завершить заявку после даты окончания аренды';
            return $this->response();
        }

        $car = $this->row->car_id;

        if (isset($car)) {
            UniModel::model('pipi_auto_calendar')
                ->where('date', '>=', Carbon::now()->toDateString())
                ->where('date', '<=', $this->row->ended_at)
                ->where('auto_id', $car)->delete();
        }

        $this->row->status = ApplicationStatus::rejected->value;
        $this->row->ended_at = Carbon::now();
        $this->row->save();

        return $this->response();
    }
}
