<?php

namespace App\Traits;

use App\Enums\Status;
use Illuminate\Database\Eloquent\Builder;

trait HasStatus
{
    public function setStatus(string $status)
    {
        if (!Status::isValid($status)) {
            throw new \InvalidArgumentException("Invalid status");
        }

        $this->status = $status;
    }

    public function setStatusSuccess()
    {
        $this->status = Status::Success;
        $this->save();
    }

    public function setStatusFailed()
    {
        $this->status = Status::Failed;
        $this->save();
    }

    public function setStatusPending()
    {
        $this->status = Status::Pending;
        $this->save();
    }

    public function scopeStatusSuccess(Builder $query): Builder
    {
        return $query->where('status', Status::Success);
    }

    public function scopeStatusFailed(Builder $query): Builder
    {
        return $query->where('status', Status::Failed);
    }

    public function scopeStatusPending(Builder $query): Builder
    {
        return $query->where('status', Status::Pending);
    }


    public function statusIsSuccessful(): bool
    {
        return $this->status === Status::Success;
    }

    public function statusHasFailed(): bool
    {
        return $this->status === Status::Failed;
    }

    public function statusIsPending(): bool
    {
        return $this->status === Status::Pending;
    }
}
