<?php

namespace App\Traits;

use Illuminate\Database\Eloquent\Builder;

trait HasStatusManagement
{
    /**
     * Check if the record can be edited
     */
    public function canEdit(): bool
    {
        return in_array($this->status, $this->getEditableStatuses());
    }

    /**
     * Check if the record can be deleted
     */
    public function canDelete(): bool
    {
        return in_array($this->status, $this->getDeletableStatuses());
    }

    /**
     * Check if the record can be cancelled
     */
    public function canCancel(): bool
    {
        return in_array($this->status, $this->getCancellableStatuses());
    }

    /**
     * Check if the record is in draft stage
     */
    public function isDraft(): bool
    {
        return $this->status === static::STATUS_DRAFT;
    }

    /**
     * Check if the record is confirmed/active
     */
    public function isConfirmed(): bool
    {
        return in_array($this->status, $this->getConfirmedStatuses());
    }

    /**
     * Check if the record is completed/finalized
     */
    public function isCompleted(): bool
    {
        return in_array($this->status, $this->getCompletedStatuses());
    }

    /**
     * Check if the record is cancelled
     */
    public function isCancelled(): bool
    {
        return $this->status === static::STATUS_CANCELLED;
    }

    /**
     * Check if the record has downstream dependencies
     */
    public function hasDownstreamDependencies(): bool
    {
        return $this->isConfirmed() || $this->isCompleted();
    }

    /**
     * Get editable statuses (usually draft only)
     */
    protected function getEditableStatuses(): array
    {
        return [static::STATUS_DRAFT];
    }

    /**
     * Get deletable statuses (usually draft only)
     */
    protected function getDeletableStatuses(): array
    {
        return [static::STATUS_DRAFT];
    }

    /**
     * Get cancellable statuses (draft and confirmed)
     */
    protected function getCancellableStatuses(): array
    {
        return [static::STATUS_DRAFT, static::STATUS_CONFIRMED];
    }

    /**
     * Get confirmed/active statuses
     */
    protected function getConfirmedStatuses(): array
    {
        return [static::STATUS_CONFIRMED, static::STATUS_IN_PROGRESS, static::STATUS_ISSUED];
    }

    /**
     * Get completed/finalized statuses
     */
    protected function getCompletedStatuses(): array
    {
        return [static::STATUS_COMPLETED, static::STATUS_FINISHED, static::STATUS_ACCEPTED];
    }

    /**
     * Scope for draft records
     */
    public function scopeDraft(Builder $query): void
    {
        $query->where('status', static::STATUS_DRAFT);
    }

    /**
     * Scope for confirmed/active records
     */
    public function scopeConfirmed(Builder $query): void
    {
        $query->whereIn('status', $this->getConfirmedStatuses());
    }

    /**
     * Scope for completed records
     */
    public function scopeCompleted(Builder $query): void
    {
        $query->whereIn('status', $this->getCompletedStatuses());
    }

    /**
     * Scope for cancellable records
     */
    public function scopeCancellable(Builder $query): void
    {
        $query->whereIn('status', $this->getCancellableStatuses());
    }

    /**
     * Scope for editable records
     */
    public function scopeEditable(Builder $query): void
    {
        $query->whereIn('status', $this->getEditableStatuses());
    }

    /**
     * Scope for deletable records
     */
    public function scopeDeletable(Builder $query): void
    {
        $query->whereIn('status', $this->getDeletableStatuses());
    }
}
