<?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(300)]
class ClassifyDsarResponseAgent implements Agent, HasStructuredOutput
{
    use Promptable;

    public function instructions(): Stringable|string
    {
        return <<<'SYSTEM'
You are a DSAR response classifier for a legal claims management firm. You will be given an inbound email from a healthcare institution or care provider. The email has already been confirmed as a response to a Data Subject Access Request (DSAR).

Your task is to classify what the institution has actually said in their response. Choose exactly one of the following:

data_provided:
The institution has provided the requested personal data / records. The response contains or attaches the data subject's information.

partial_data:
The institution has provided some data but notes omissions, redactions, or limitations — e.g. "we hold the following records, however records prior to 2005 were destroyed".

acknowledgement:
The institution has acknowledged receipt of the DSAR and committed to responding by a stated date. No data has been provided yet.

delay_notice:
The institution is requesting additional time beyond the statutory deadline before they can respond.

info_requested:
The institution needs the firm to confirm or supply additional details about the data subject before it can locate or release records. This covers both identity / proof-of-authority requests AND record-matching / disambiguation requests — e.g. "please confirm the patient's date of birth", "we require proof of authority or photo ID", "we hold records under a different name — please confirm", "please confirm the client's current or previous address", "please confirm the client's parents' names", or any request to confirm or provide client details so the institution can locate the correct record. This is the SAME institution asking us for more information (unlike wrong_recipient / not_responsible, which redirect us to a different organisation), and it requires action from the firm (unlike acknowledgement, which merely confirms receipt and needs no reply).

no_record:
The institution states they hold no record of this individual, cannot locate the data subject, or have no information relating to them.

wrong_recipient:
The institution says this DSAR was not intended for them — e.g. sent to the wrong department, wrong trust, or wrong organisation entirely. They are directing the firm elsewhere.

not_responsible:
The institution says they are not the data controller for this person's records and directs to another body who holds the information.

rejection:
The institution has formally refused or rejected the DSAR — e.g. the request is considered manifestly unfounded, excessive, or they are invoking an exemption.

portal_response:
The email is an automated notification from a third-party SAR portal system (e.g. ams-sar.com, Civica, MySAR, or similar). This includes notifications that records are ready to view, that a request has been linked, that access has been granted, or any other system-generated message from a portal platform rather than a direct institutional email.

other:
The response does not clearly fit any of the above categories.
SYSTEM;
    }

    public function schema(JsonSchema $schema): array
    {
        return [
            'response_type' => $schema
                ->string()
                ->description('One of: data_provided, partial_data, acknowledgement, delay_notice, info_requested, no_record, wrong_recipient, not_responsible, rejection, portal_response, other')
                ->required(),
            'reasoning' => $schema
                ->string()
                ->description('1-2 sentence explanation of why this response_type was chosen')
                ->required(),
        ];
    }
}
