<?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 for a legal claims management firm. You will be given an inbound email from a healthcare or care institution responding to a Data Subject Access Request (DSAR).

Extract TWO types of reference from the email:

1. OUR DSAR REFERENCE (dsar_reference):
   Look for the solicitor's own reference number — always in the format "DSAR-YYYYMMDD-XXXXXX", e.g. "DSAR-20260603-3B0EFD".
   It commonly appears in:
   - The email subject line: "[Ref: DSAR-20260603-3B0EFD]"
   - Quoted text in the body: "Your reference: DSAR-20260603-3B0EFD"
   - "Your ref: [X]" or "Re: [X]" where X looks like a DSAR reference code
   If found, return the full reference exactly as written. If not found, return an empty string.

2. INSTITUTION REFERENCE (inst_reference):
   Look for the institution's own case reference for this request.
   Look for patterns such as:
   - "Our reference: IGO-2024-1234"
   - "Our ref / Case ref: FOI/2024/056"
   - "Case number: SAR/2024/00123" or "CAS-98765"
   - "Request ID: REQ-20240501"
   - "The request has been given the unique reference number [X]"
   - Any alphanumeric code clearly labelled as their reference
   Do NOT return patient NHS numbers, dates of birth, or the solicitor's DSAR reference here.
   If found, return the reference. If not found, return an empty string.

IMPORTANT — scan the ENTIRE email, including:
- All quoted/replied thread content (lines prefixed with >, forwarded blocks, "On [date] [someone] wrote:" sections)
- Any ATTACHMENT sections (labelled "--- ATTACHMENT: filename ---" in this prompt)
If the institution reference is not in the email body, check the attachment sections before returning an empty string.

confidence: your confidence in the extracted values (high/medium/low).
If neither reference is found, return empty strings for both and confidence = low.
SYSTEM;
    }

    public function schema(JsonSchema $schema): array
    {
        return [
            'dsar_reference' => $schema->string()
                ->description('Our DSAR reference in format DSAR-YYYYMMDD-XXXXXX, or empty string if not found')
                ->required(),
            'inst_reference' => $schema->string()
                ->description('The institution\'s own case reference, or empty string if not found')
                ->required(),
            'confidence' => $schema->string()
                ->enum(['high', 'medium', 'low'])
                ->required(),
        ];
    }
}
