<?php

namespace Database\Factories;

use App\Models\SopTemplate;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\SopTemplate>
 */
class SopTemplateFactory extends Factory
{
    protected $model = SopTemplate::class;

    public function definition(): array
    {
        return [
            'name' => fake()->randomElement([
                '设备启动操作规程',
                '产品质检标准流程',
                '安全生产检查清单',
                '设备维护保养流程',
                '应急处理操作指南',
            ]) . '-' . fake()->numberBetween(1, 100),
            'description' => fake()->sentence(20),
            'category' => fake()->randomElement(['生产操作', '质量管理', '安全管理', '设备维护', '应急处理']),
            'tags' => fake()->randomElements(['标准作业', '安全', '质量', '效率', '培训'], fake()->numberBetween(1, 3)),
            'version' => '1.0.0',
            'status' => fake()->randomElement(['draft', 'published', 'archived']),
            'applicable_departments' => fake()->randomElements(['生产部', '质检部', '设备部', '安全部'], fake()->numberBetween(1, 2)),
            'applicable_positions' => fake()->randomElements(['操作员', '质检员', '班组长', '技术员'], fake()->numberBetween(1, 2)),
            'published_at' => fake()->optional(0.6)->dateTimeBetween('-6 months', 'now'),
            'created_by' => User::factory(),
        ];
    }

    public function draft(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => 'draft',
            'published_at' => null,
        ]);
    }

    public function published(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => 'published',
            'published_at' => now(),
        ]);
    }

    public function archived(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => 'archived',
            'published_at' => fake()->dateTimeBetween('-1 year', '-1 month'),
        ]);
    }
}
