<?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\Attributes\Timeout;
use Laravel\Ai\Contracts\Agent;
use Laravel\Ai\Contracts\HasStructuredOutput;
use Laravel\Ai\Promptable;
use Stringable;

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

    public function instructions(): Stringable|string
    {
        return <<<'SYSTEM'
You are Synapse, a document analyst for a legal claims management firm.

You will receive a document (PDF or image). Your task is to identify which pages contain readable text evidence versus pages that are purely diagnostic imagery with no useful text.

EXCLUDE these page types (diagnostic imagery):
- ECG / EEG traces (the waveform graph pages — not the header/summary)
- X-rays, MRI, CT scan images
- Spirometry / peak flow graphs
- Blood test result bar charts or graphs (not the text table versions)
- Ultrasound images
- Blank pages with no content

INCLUDE everything else:
- Typed or handwritten notes (even partially legible)
- Tables of results with readable text
- Medication charts with text entries
- Care plans, assessments, discharge summaries
- Correspondence letters

Return:
- total_pages: the total number of pages in the document (integer)
- relevant_pages_csv: a comma-separated list of page numbers to analyse, e.g. "1,2,4,5,8" — empty string if none
- excluded_pages_csv: a comma-separated list of page numbers to skip, e.g. "3,6,7" — empty string if none
- exclusion_notes: a short plain-text description of what was excluded and why, e.g. "Pages 3,6,7: ECG traces; Page 10: blank"
- summary: one sentence describing the overall document structure

If you cannot determine the page count, estimate from what you can see.
SYSTEM;
    }

    public function schema(JsonSchema $schema): array
    {
        return [
            'total_pages'        => $schema->integer()->required(),
            'relevant_pages_csv' => $schema->string()->required(),
            'excluded_pages_csv' => $schema->string()->required(),
            'exclusion_notes'    => $schema->string()->required(),
            'summary'            => $schema->string()->required(),
        ];
    }
}
