<?php

namespace App\Domain\Project\Models;

use App\Domain\Crew\Models\Crew;
use App\Domain\Email\Models\EmailTemplate;
use App\Domain\Project\Enums\ProjectExecutionMode;
use App\Domain\Project\Enums\ProjectStatus;
use App\Domain\Project\Enums\ProjectType;
use App\Domain\Shared\Traits\BelongsToTeam;
use App\Domain\Shared\Traits\HasPluginMeta;
use App\Domain\Website\Models\Website;
use App\Domain\Workflow\Models\Workflow;
use App\Models\User;
use Database\Factories\Domain\Project\ProjectFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Carbon;

/**
 * @property string $id
 * @property string|null $team_id
 * @property string|null $user_id
 * @property string $title
 * @property string|null $description
 * @property ProjectType $type
 * @property ProjectExecutionMode $execution_mode
 * @property ProjectStatus $status
 * @property string|null $paused_from_status
 * @property string|null $goal
 * @property string|null $crew_id
 * @property string|null $workflow_id
 * @property string|null $website_id
 * @property string|null $email_template_id
 * @property array<string, mixed>|null $agent_config
 * @property array<string, mixed>|null $budget_config
 * @property array<string, mixed>|null $notification_config
 * @property array<string, mixed>|null $delivery_config
 * @property array<string, mixed>|null $settings
 * @property array<int, string>|null $allowed_tool_ids
 * @property array<int, string>|null $allowed_credential_ids
 * @property int $total_runs
 * @property int $successful_runs
 * @property int $failed_runs
 * @property int $total_spend_credits
 * @property Carbon|null $started_at
 * @property Carbon|null $paused_at
 * @property Carbon|null $completed_at
 * @property Carbon|null $last_run_at
 * @property Carbon|null $next_run_at
 * @property array<string, mixed>|null $meta
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property-read User|null $user
 * @property-read ProjectSchedule|null $schedule
 */
class Project extends Model
{
    use BelongsToTeam, HasFactory, HasPluginMeta, HasUuids;

    protected static function newFactory()
    {
        return ProjectFactory::new();
    }

    /**
     * Projects this project depends on (upstream).
     */
    public function dependencies(): HasMany
    {
        return $this->hasMany(ProjectDependency::class)->orderBy('sort_order');
    }

    /**
     * Projects that depend on this project (downstream).
     */
    public function dependents(): HasMany
    {
        return $this->hasMany(ProjectDependency::class, 'depends_on_id');
    }

    protected $fillable = [
        'team_id',
        'user_id',
        'title',
        'description',
        'type',
        'execution_mode',
        'status',
        'paused_from_status',
        'goal',
        'crew_id',
        'workflow_id',
        'website_id',
        'agent_config',
        'budget_config',
        'notification_config',
        'delivery_config',
        'settings',
        'allowed_tool_ids',
        'allowed_credential_ids',
        'email_template_id',
        'total_runs',
        'successful_runs',
        'failed_runs',
        'total_spend_credits',
        'started_at',
        'paused_at',
        'completed_at',
        'last_run_at',
        'next_run_at',
        'meta',
    ];

    protected function casts(): array
    {
        return [
            'meta' => 'array',
            'type' => ProjectType::class,
            'execution_mode' => ProjectExecutionMode::class,
            'status' => ProjectStatus::class,
            'agent_config' => 'array',
            'budget_config' => 'array',
            'notification_config' => 'array',
            'delivery_config' => 'array',
            'settings' => 'array',
            'allowed_tool_ids' => 'array',
            'allowed_credential_ids' => 'array',
            'total_runs' => 'integer',
            'successful_runs' => 'integer',
            'failed_runs' => 'integer',
            'total_spend_credits' => 'integer',
            'started_at' => 'datetime',
            'paused_at' => 'datetime',
            'completed_at' => 'datetime',
            'last_run_at' => 'datetime',
            'next_run_at' => 'datetime',
        ];
    }

    // Relationships

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

    public function crew(): BelongsTo
    {
        return $this->belongsTo(Crew::class);
    }

    public function workflow(): BelongsTo
    {
        return $this->belongsTo(Workflow::class);
    }

    public function website(): BelongsTo
    {
        return $this->belongsTo(Website::class);
    }

    public function emailTemplate(): BelongsTo
    {
        return $this->belongsTo(EmailTemplate::class);
    }

    public function schedule(): HasOne
    {
        return $this->hasOne(ProjectSchedule::class);
    }

    public function runs(): HasMany
    {
        return $this->hasMany(ProjectRun::class)->orderByDesc('run_number');
    }

    public function milestones(): HasMany
    {
        return $this->hasMany(ProjectMilestone::class)->orderBy('sort_order');
    }

    // Scopes

    public function scopeActive(Builder $query): Builder
    {
        return $query->where('status', ProjectStatus::Active);
    }

    public function scopeContinuous(Builder $query): Builder
    {
        return $query->where('type', ProjectType::Continuous);
    }

    public function scopeOneShot(Builder $query): Builder
    {
        return $query->where('type', ProjectType::OneShot);
    }

    // Helpers

    public function latestRun(): ?ProjectRun
    {
        return $this->runs()->first();
    }

    public function activeRun(): ?ProjectRun
    {
        return $this->runs()
            ->whereIn('status', ['pending', 'running'])
            ->first();
    }

    public function successRate(): float
    {
        if ($this->total_runs === 0) {
            return 0;
        }

        return round(($this->successful_runs / $this->total_runs) * 100, 1);
    }

    public function consecutiveFailures(): int
    {
        return $this->runs()
            ->orderByDesc('run_number')
            ->orderByDesc('created_at')
            ->get()
            ->takeWhile(fn (ProjectRun $run) => $run->status->value === 'failed')
            ->count();
    }

    public function isOverBudget(string $period = 'daily'): bool
    {
        $cap = $this->budget_config[$period.'_cap'] ?? null;
        if (! $cap) {
            return false;
        }

        return $this->periodSpend($period) >= $cap;
    }

    public function periodSpend(string $period): int
    {
        $since = match ($period) {
            'daily' => now()->startOfDay(),
            'weekly' => now()->startOfWeek(),
            'monthly' => now()->startOfMonth(),
            default => now()->startOfDay(),
        };

        return $this->runs()
            ->where('created_at', '>=', $since)
            ->sum('spend_credits');
    }

    public function isContinuous(): bool
    {
        return $this->type === ProjectType::Continuous;
    }

    public function isOneShot(): bool
    {
        return $this->type === ProjectType::OneShot;
    }
}
