<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class OCRRawData extends Model
{
    use HasFactory;

    protected $table = 'ocr_raw_data';

    protected $fillable = [
        'ocr_record_id',
        'raw_response',
        'parsed_blocks',
        'api_request_id',
        'algo_version',
        'total_blocks',
        'metadata',
    ];

    protected $casts = [
        'raw_response' => 'array',
        'parsed_blocks' => 'array',
        'metadata' => 'array',
    ];

    /**
     * 关联OCR记录
     */
    public function ocrRecord()
    {
        return $this->belongsTo(OCRRecord::class);
    }

    /**
     * 获取文本块数据
     */
    public function getTextBlocks(): array
    {
        return $this->parsed_blocks ?? [];
    }

    /**
     * 保存原始API响应
     */
    public static function saveRawResponse(int $ocrRecordId, array $response): self
    {
        // 提取文本块
        $blocks = [];
        $totalBlocks = 0;
        $requestId = null;
        $algoVersion = null;

        if (isset($response['data'])) {
            $requestId = $response['requestId'] ?? null;
            $algoVersion = $response['data']['algo_version'] ?? null;

            if (isset($response['data']['page_list'])) {
                foreach ($response['data']['page_list'] as $page) {
                    if (isset($page['answer_list'])) {
                        foreach ($page['answer_list'] as $item) {
                            if (isset($item['content_list_info'])) {
                                foreach ($item['content_list_info'] as $content) {
                                    if (isset($content['text']) && !empty(trim($content['text']))) {
                                        $blocks[] = [
                                            'text' => trim($content['text']),
                                            'position' => $content['pos'] ?? null,
                                            'confidence' => $content['confidence'] ?? null,
                                            'doc_index' => $content['doc_index'] ?? null,
                                            'type' => null // 将在OCRDataParser中识别
                                        ];
                                        $totalBlocks++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        return self::create([
            'ocr_record_id' => $ocrRecordId,
            'raw_response' => $response,
            'parsed_blocks' => $blocks,
            'api_request_id' => $requestId,
            'algo_version' => $algoVersion,
            'total_blocks' => $totalBlocks,
            'metadata' => [
                'saved_at' => now()->toISOString(),
                'page_count' => count($response['data']['page_list'] ?? []),
            ]
        ]);
    }
}
