<?php

declare(strict_types=1);

namespace Database\Seeders;

use App\Models\HealthTreatment;
use App\Models\Product;
use App\Models\Report;
use Database\Seeders\Support\DemoSeederContext;
use Illuminate\Database\Seeder;

class DemoHealthTreatmentsSeeder extends Seeder
{
    public function run(): void
    {
        $admin = DemoSeederContext::admin();
        $producer = DemoSeederContext::producer();
        $product = Product::query()->first();
        $project = DemoSeederContext::mainProject();

        $healthReport = Report::query()
            ->where('type', Report::TYPE_HEALTH)
            ->where('report_type', Report::HEALTH_TYPE_TREATMENT)
            ->first();

        if (! $admin || ! $producer || ! $product || ! $project) {
            return;
        }

        HealthTreatment::firstOrCreate(
            [
                'project_id' => $project->id,
                'product_id' => $product->id,
                'initial_report_id' => $healthReport?->id,
            ],
            [
                'producer_id' => $producer->id,
                'status' => HealthTreatment::STATUS_UNDER_TREATMENT,
                'disease_name' => $healthReport?->disease_name ?? 'تب برفکی',
                'started_at' => now()->subDays(5),
                'next_visit_at' => now()->addDays(7),
                'progress_percent' => 35,
                'notes' => 'درمان آغاز شده — پیگیری هفتگی',
                'created_by' => $admin->id,
                'updated_by' => $admin->id,
            ]
        );

        HealthTreatment::firstOrCreate(
            [
                'project_id' => $project->id,
                'product_id' => $product->id,
                'status' => HealthTreatment::STATUS_COMPLETED,
            ],
            [
                'producer_id' => $producer->id,
                'disease_name' => 'عفونت سم',
                'started_at' => now()->subMonths(2),
                'ended_at' => now()->subMonth(),
                'progress_percent' => 100,
                'notes' => 'درمان با موفقیت تکمیل شد',
                'created_by' => $admin->id,
                'updated_by' => $admin->id,
            ]
        );
    }
}
