<?php

namespace App\Models;

use App\Components\Functions;
use App\Events\LogAction;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class RoomReservation extends Model
{
    //
    use SoftDeletes;

    protected $fillable = [
        'name',
        'phone',
        'room_id',
        'hostel_id',
        'id_number',
        'email',
        'deposit',
        'date_join',
        'created_at',
        'updated_at',
        'deleted_at',
        'bed_id',
        'user_id',
        'note',
        'price_deal'
    ];

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

    public function room()
    {
        return $this->belongsTo(Room::class);
    }

    public function hostel()
    {
        return $this->belongsTo(Hostel::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function bed()
    {
        return $this->belongsTo(RoomBed::class, 'bed_id', 'id');
    }

    public function collectSpends()
    {
        return $this->hasMany(CollectSpend::class, 'reserve_id', 'id');
    }

    public function files()
    {
        return $this->hasMany(ReserveFiles::class, 'reserve_id', 'id');
    }

    public static function boot()
    {
        parent::boot();
        static::deleted(function ($item) {
            Deposit::query()->where('reserve_id', $item->id)->delete();
        });

        static::created(function ($item) {
            Deposit::create([
                'reserve_id' => $item->id,
                'amount' => $item->deposit,
                'date_action' => $item->created_at,
                'room_id' => $item->room_id,
                'hostel_id' => $item->hostel_id,
            ]);

            $user = Functions::getCurrentUser();
            $desc = '{' . optional($user)->name . '} nhận cọc giữ chỗ phòng {' . $item->room->name . '} nhà {' . $item->room->hostel->name . '} số tiền {' . number_format($item->deposit, 0, '.', '.') . '}';
            if ($item->hostel->type_rent == Hostel::TYPE_RENT_EVERY) {
                $desc = '{' . optional($user)->name . '} nhận cọc giữ chỗ giường {' . optional($item->bed)->name . '}phòng {' . $item->room->name . '} nhà {' . $item->room->hostel->name . '} số tiền {' . number_format($item->deposit, 0, '.', '.') . '}';
            }

            event(new LogAction([
                'type' => 'create-reserve',
                'user_id' => optional($user)->id,
                'object_id' => $item->id,
                'hostel_id' => $item->hostel_id,
                'room_id' => $item->room_id,
                'properties' => $item->toArray(),
                'desc' => $desc
            ]));
        });

        static::updated(function ($item) {
            Deposit::query()
                ->where('reserve_id', $item->id)
                ->update([
                    'amount' => $item->deposit
                ]);
        });
    }


}
