{{!
    This file is part of Moodle - http://moodle.org/

    Moodle is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Moodle is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
}}
{{!
    @template core_ai/action_detail

    Full detail of a single logged AI action.

    Context variables required for this template:
    * id - The id of the ai_action_register row.
    * actionname - The display name of the action type.
    * provider - The display name of the AI provider.
    * model - The model used, if known.
    * success - Whether the action completed successfully.
    * statustext - Display text for the success/failure status.
    * errormessage - The error message, if the action failed.
    * timecreated - Display formatted time the action was created.
    * timecompleted - Display formatted time the action completed, if known.
    * contextname - The display name of the closest resolvable context, if any.
    * contexturl - The URL of the closest resolvable context, if any.
    * userfullname - The display name of the user who made the request, if known.
    * userprofileurl - The URL of the user's profile, if known.
    * istext - Whether this action stores a text prompt and generated text response.
    * isimage - Whether this action stores an image generation prompt and result.
    * prompt - The raw prompt text sent to the provider.
    * generatedcontent - The raw text generated by the provider (text actions only).
    * prompttokens - The number of prompt tokens used (text actions only).
    * completiontokens - The number of completion tokens used (text actions only).
    * revisedprompt - The provider-revised prompt, if any (image actions only).
    * sourceurl - The URL of the generated image, if any (image actions only).
    * quality - The requested image quality (image actions only).
    * aspectratio - The requested image aspect ratio, if any (image actions only).
    * style - The requested image style, if any (image actions only).
    * numberimages - The number of images requested (image actions only).

    Example context (json):
    {
        "id": 42,
        "actionname": "Generate text",
        "provider": "OpenAI",
        "model": "gpt-4",
        "success": true,
        "statustext": "Yes",
        "timecreated": "1 January 2026, 9:00 AM",
        "timecompleted": "1 January 2026, 9:00 AM",
        "contextname": "Forum: AI usage test forum",
        "contexturl": "http://example.com/mod/forum/view.php?id=1",
        "userfullname": "Admin User",
        "userprofileurl": "http://example.com/user/profile.php?id=2",
        "istext": true,
        "isimage": false,
        "prompt": "Write a haiku about Moodle.",
        "generatedcontent": "Modules stack like blocks...",
        "prompttokens": 12,
        "completiontokens": 20
    }
}}
<div class="core-ai-action-detail">
    <div class="card mb-3">
        <div class="card-body">
            <h3 class="card-title h5">{{#str}}actiondetailsummary, core_ai{{/str}}</h3>
            {{^success}}
                <div class="alert alert-danger" role="alert">
                    <strong>{{#str}}error, moodle{{/str}}:</strong> {{errormessage}}
                </div>
            {{/success}}
            <dl class="row mb-0">
                <dt class="col-sm-3">{{#str}}action, core_ai{{/str}}</dt>
                <dd class="col-sm-9">{{actionname}}</dd>

                <dt class="col-sm-3">{{#str}}provider, core_ai{{/str}}</dt>
                <dd class="col-sm-9">{{provider}}</dd>

                {{#model}}
                    <dt class="col-sm-3">{{#str}}model, core_ai{{/str}}</dt>
                    <dd class="col-sm-9">{{model}}</dd>
                {{/model}}

                <dt class="col-sm-3">{{#str}}success, moodle{{/str}}</dt>
                <dd class="col-sm-9">
                    <span class="{{#success}}text-success{{/success}}{{^success}}text-danger{{/success}}">
                        {{statustext}}
                    </span>
                </dd>

                <dt class="col-sm-3">{{#str}}timegenerated, core_ai{{/str}}</dt>
                <dd class="col-sm-9">{{timecreated}}</dd>

                {{#timecompleted}}
                    <dt class="col-sm-3">{{#str}}timecompleted, core_ai{{/str}}</dt>
                    <dd class="col-sm-9">{{timecompleted}}</dd>
                {{/timecompleted}}

                <dt class="col-sm-3">{{#str}}fullnameuser, moodle{{/str}}</dt>
                <dd class="col-sm-9">
                    {{#userfullname}}
                        <a href="{{userprofileurl}}">{{userfullname}}</a>
                    {{/userfullname}}
                    {{^userfullname}}
                        {{#str}}unknownvalue, core_ai{{/str}}
                    {{/userfullname}}
                </dd>

                <dt class="col-sm-3">{{#str}}contextname, moodle{{/str}}</dt>
                <dd class="col-sm-9">
                    {{#contextname}}
                        <a href="{{contexturl}}">{{contextname}}</a>
                    {{/contextname}}
                    {{^contextname}}
                        {{#str}}unknownvalue, core_ai{{/str}}
                    {{/contextname}}
                </dd>
            </dl>
        </div>
    </div>

    {{#istext}}
        <div class="card mb-3">
            <div class="card-body">
                <h3 class="card-title h5">{{#str}}prompt, core_ai{{/str}}</h3>
                <pre class="core-ai-action-detail-text p-3 bg-light border rounded">{{prompt}}</pre>
            </div>
        </div>
        <div class="card mb-3">
            <div class="card-body">
                <h3 class="card-title h5">{{#str}}generatedcontent, core_ai{{/str}}</h3>
                <pre class="core-ai-action-detail-text p-3 bg-light border rounded">{{generatedcontent}}</pre>
                <p class="text-muted small mb-0">
                    {{#str}}prompttokens, core_ai{{/str}}: {{prompttokens}}
                    &bull;
                    {{#str}}completiontokens, core_ai{{/str}}: {{completiontokens}}
                </p>
            </div>
        </div>
    {{/istext}}

    {{#isimage}}
        <div class="card mb-3">
            <div class="card-body">
                <h3 class="card-title h5">{{#str}}prompt, core_ai{{/str}}</h3>
                <pre class="core-ai-action-detail-text p-3 bg-light border rounded">{{prompt}}</pre>
                {{#revisedprompt}}
                    <h4 class="h6 mt-3">{{#str}}revisedprompt, core_ai{{/str}}</h4>
                    <pre class="core-ai-action-detail-text p-3 bg-light border rounded">{{revisedprompt}}</pre>
                {{/revisedprompt}}
            </div>
        </div>
        <div class="card mb-3">
            <div class="card-body">
                <h3 class="card-title h5">{{#str}}generatedimage, core_ai{{/str}}</h3>
                {{#sourceurl}}
                    <img src="{{sourceurl}}" alt="{{#str}}generatedimage, core_ai{{/str}}"
                        class="core-ai-action-detail-image img-fluid border rounded mb-2">
                {{/sourceurl}}
                {{^sourceurl}}
                    <p>{{#str}}noimageavailable, core_ai{{/str}}</p>
                {{/sourceurl}}
                <p class="text-muted small mb-0">
                    {{#str}}imagequality, core_ai{{/str}}: {{quality}}
                    {{#aspectratio}}&bull; {{#str}}imageaspectratio, core_ai{{/str}}: {{aspectratio}}{{/aspectratio}}
                    {{#style}}&bull; {{#str}}imagestyle, core_ai{{/str}}: {{style}}{{/style}}
                </p>
            </div>
        </div>
    {{/isimage}}
</div>
