<?php

namespace Database\Factories;

use App\Models\Document;
use App\Models\KnowledgeBase;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

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

    public function definition(): array
    {
        $faker = \Faker\Factory::create('zh_CN');

        $titles = [
            '项目管理规范', '技术文档模板', '员工手册', '产品需求文档',
            '系统设计方案', '测试报告', '会议纪要', '培训资料',
            '操作指南', '年度总结报告',
        ];

        $title = $faker->randomElement($titles) . ' - ' . $faker->word();
        $fileName = $faker->word() . '_' . date('Ymd') . '.docx';

        return [
            'title' => $title,
            'description' => $faker->paragraph(3),
            'file_path' => 'documents/' . date('Y/m/d') . '/' . fake()->uuid() . '.docx',
            'file_name' => $fileName,
            'file_size' => fake()->numberBetween(10000, 5000000),
            'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            'knowledge_base_id' => KnowledgeBase::factory(),
            'uploaded_by' => User::factory(),
            'markdown_path' => null,

            'conversion_status' => 'pending',
            'conversion_error' => null,
        ];
    }

    /**
     * 指定文档已完成转换
     */
    public function converted(): static
    {
        $faker = \Faker\Factory::create('zh_CN');
        $uuid = fake()->uuid();

        return $this->state(fn (array $attributes) => [
            'markdown_path' => 'markdown/' . date('Y/m/d') . '/' . $uuid . '.md',

            'conversion_status' => 'completed',
            'conversion_error' => null,
        ]);
    }

    /**
     * 指定文档转换失败
     */
    public function conversionFailed(): static
    {
        return $this->state(fn (array $attributes) => [
            'markdown_path' => null,

            'conversion_status' => 'failed',
            'conversion_error' => 'Failed to convert document: Invalid file format',
        ]);
    }

    /**
     * 指定文档正在转换中
     */
    public function converting(): static
    {
        return $this->state(fn (array $attributes) => [
            'markdown_path' => null,

            'conversion_status' => 'processing',
            'conversion_error' => null,
        ]);
    }
}
