<?php

namespace App\Models;

use App\Components\Functions;
use App\Jobs\DeleteRenterConversation;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Traits\LogsActivity;

class RenterRoom extends Model
{
    //
    use SoftDeletes;
    use LogsActivity;

    protected static $logAttributes = ['*'];

    const RESIDENCE_NOT_DECLARE = 0;
    const RESIDENCE_LIMIT = 1;
    const RESIDENCE_NOT_LIMIT = 2;

    const BICYCLE = 0;
    const MOTORBIKE = 1;
    const ELECTRIC_BIKE = 2;
    const CAR = 3;

    protected $fillable = [
        'contract_id',
        'type_rent',
        'room_id',
        'hostel_id',
        'user_id',
        'created_at',
        'updated_at',
        'deleted_at',
        'date_joined',
        'residence_status',
        'date_end_residence',
        'room_contract_id',
        'bed_id'
    ];

    protected $dates = [
        'created_at',
        'updated_at',
        'deleted_at',
        'date_end_residence',
        'date_joined'
    ];

    public function room()
    {
        return $this->belongsTo('App\Models\Room', 'room_id', 'id');
    }

    public function account()
    {
        return $this->belongsTo('App\User', 'user_id', 'id');
    }

    public function hostel()
    {
        return $this->belongsTo('App\Models\Hostel', 'hostel_id', 'id');
    }

    public function contract()
    {
        return $this->belongsTo('App\Models\Contract', 'contract_id', 'id');
    }

    public function roomContract()
    {
        return $this->belongsTo('App\Models\Contract', 'room_contract_id', 'id');
    }

    public function renter()
    {
        return $this->belongsTo(Renter::class, 'user_id', 'user_id');
    }

    public static function boot()
    {
        parent::boot();
        static::saved(function ($item) {
            $hostel = $item->hostel;
            if ($hostel) {
                Functions::updateHostelEmptyRooms($hostel->id);
            }

//			$user = $item->account;
//			if ( $user ) {
//				$user->current_hostel = $item->hostel_id;
//				$user->save();
//			}
        });
        static::deleted(function ($renterRoom) {

            $contractId = $renterRoom->contract_id;
            $item = $renterRoom->contract;
            if (!$item) {
                return;
            }
            $hostel = $item->hostel;
            $roomId = $item->room_id;
            $collectSpends = CollectSpend::query()->where('contract_id', $contractId)->get();
            $transactionIds = $collectSpends->pluck('transaction_id')->toArray();
            Transaction::query()->whereIn('id', $transactionIds)->delete();
            CollectSpend::query()->where('contract_id', $contractId)->delete();
            $moneyInfos = MoneyInfo::where('contract_id', $contractId)->get();
            $moneyInfoArrs = $moneyInfos->pluck('id')->toArray();
            MoneyDetail::query()->whereIn('money_info_id', $moneyInfoArrs)->delete();
            CollectSpend::query()->whereIn('money_info_id', $moneyInfoArrs)->delete();
            Statistic::query()->whereIn('money_info_id', $moneyInfoArrs)->delete();

            $room = $item->room;

            MoneyInfo::query()->where('contract_id', $contractId)->delete();

            if ($item->status != Contract::LIQUIDATED) {
                RenterBike::query()
                    ->where('renter_id', $item->renter_id)
                    ->where('room_id', $roomId)
                    ->delete();
                $renter = Renter::query()
                    ->where('user_id', $item->renter_id)
                    ->where('room_id', $roomId)
                    ->first();
                if ($renter) {
                    $renter->delete();
                }

                $renterRoom = RenterRoom::query()
                    ->where('user_id', $item->renter_id)
                    ->where('room_id', $roomId)
                    ->first();
                if ($renterRoom) {
                    $renterRoom->delete();
                }

                if ($room->hostel->type_rent == Hostel::TYPE_RENT_ALL) {
                    //RenterRoom::query()->where( 'room_id', $roomId )->delete();
                    RenterBike::query()->where('room_id', $roomId)->delete();
                    //Renter::query()->where( 'room_id', $roomId )->delete();
                }
            }

            StatisticLog::query()->where('contract_id', $contractId)->delete();

            if ($hostel->type_rent == Hostel::TYPE_RENT_EVERY) {
                $bed = RoomBed::find($item->bed_id);
                if ($bed) {
                    $bed->update([
                        'status' => RoomBed::AVAILABLE,
                    ]);
                }
            }

            dispatch(new DeleteRenterConversation($item->renter_id, $item->room_id));
        });


    }
}
