<?php

namespace Database\Factories;

use App\Models\Terminal;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Terminal>
 */
class TerminalFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Terminal::class;

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'name' => fake()->randomElement(['生产线A', '生产线B', '生产线C', '质检站', '包装站']) . '-' . fake()->randomElement(['工位1', '工位2', '工位3']),
            'code' => 'TERM-' . fake()->unique()->numerify('####'),
            'ip_address' => fake()->localIpv4(),
            'station_id' => null,
            'diagram_urls' => [['title' => '组态', 'url' => fake()->url()]],
            'prompt_template' => null,
            'voice_wakeup_enabled' => false,
            'voice_wakeup_word' => null,
            'is_online' => fake()->boolean(70), // 70%概率在线
            'last_online_at' => fake()->dateTimeBetween('-7 days', 'now'),
        ];
    }

    /**
     * 指定终端为在线状态
     */
    public function online(): static
    {
        return $this->state(fn (array $attributes) => [
            'is_online' => true,
            'last_online_at' => now(),
        ]);
    }

    /**
     * 指定终端为离线状态
     */
    public function offline(): static
    {
        return $this->state(fn (array $attributes) => [
            'is_online' => false,
            'last_online_at' => fake()->dateTimeBetween('-30 days', '-1 day'),
        ]);
    }

    /**
     * 指定终端为生产线终端
     */
    public function productionLine(): static
    {
        return $this->state(fn (array $attributes) => [
            'name' => '生产线' . fake()->randomElement(['A', 'B', 'C', 'D']) . '-工位' . fake()->numberBetween(1, 10),
            'code' => 'PROD-' . fake()->unique()->numerify('####'),
        ]);
    }

    /**
     * 指定终端为质检站终端
     */
    public function qualityControl(): static
    {
        return $this->state(fn (array $attributes) => [
            'name' => '质检站-' . fake()->randomElement(['入库检', '出库检', '过程检']) . '-' . fake()->numberBetween(1, 5),
            'code' => 'QC-' . fake()->unique()->numerify('####'),
        ]);
    }

    /**
     * 指定终端为包装站终端
     */
    public function packaging(): static
    {
        return $this->state(fn (array $attributes) => [
            'name' => '包装站-工位' . fake()->numberBetween(1, 8),
            'code' => 'PKG-' . fake()->unique()->numerify('####'),
        ]);
    }
}
