<?php

namespace App\Synapse\Agents;

use Illuminate\Contracts\JsonSchema\JsonSchema;
use Laravel\Ai\Attributes\MaxTokens;
use Laravel\Ai\Attributes\Model;
use Laravel\Ai\Attributes\Provider;
use Laravel\Ai\Contracts\Agent;
use Laravel\Ai\Contracts\HasStructuredOutput;
use Laravel\Ai\Promptable;
use Stringable;

#[Provider('openai')]
#[Model('gpt-4o-mini')]
#[MaxTokens(150)]
class ExtractDsarReferenceAgent implements Agent, HasStructuredOutput
{
    use Promptable;

    public function instructions(): Stringable|string
    {
        return <<<'SYSTEM'
You are a reference number extractor. You will be given an inbound email from a healthcare institution responding to a Data Subject Access Request (DSAR).

Your sole task is to extract the institution's own reference number for this case, if one exists.

Look for patterns such as:
- "Our reference: IGO-2024-1234"
- "Your reference / Our ref: FOI/2024/056"
- "Case number: CAS-98765"
- "Reference: SAR/2024/00123"
- "Request ID: REQ-20240501"
- A standalone alphanumeric code clearly labelled as a reference

Do NOT return:
- The solicitor's own reference number
- Subject line alone
- Patient NHS numbers or dates of birth
- A reference if you are not confident it is the institution's own case reference

If no clear reference exists, return null.
SYSTEM;
    }

    public function schema(JsonSchema $schema): array
    {
        return [
            'reference' => $schema
                ->string()
                ->nullable()
                ->description('The institution\'s own reference/case number for this DSAR, or null if not found')
                ->required(),
            'confidence' => $schema
                ->string()
                ->description('high | medium | low — how confident you are this is the correct reference')
                ->required(),
        ];
    }
}
