{% extends "base.html" %}
{% block title_suffix %} - {{ req.reqname }}{% endblock %}

{% block content %}
<div class="page-header">
    <h1>
        <img src="{{ url_for('static', filename='icons/ui_viewdetails.png') }}" alt="">
        {{ req.reqname }}
    </h1>
    <div class="button-group">
        <a href="{{ url_for('request_edit', reqid=req.reqid) }}" class="btn">
            <img src="{{ url_for('static', filename='icons/ui_editrequest.png') }}" alt="">
            Edit
        </a>
        <form action="{{ url_for('request_delete_route', reqid=req.reqid) }}" method="POST" style="display:inline;"
              onsubmit="return confirm('Delete request {{ req.reqname }}?');">
            <button type="submit" class="btn btn-danger">Delete</button>
        </form>
    </div>
</div>

<div class="detail-card">
    <dl class="detail-list">
        <dt>ID</dt>
        <dd>{{ req.reqid }}</dd>

        <dt>Project</dt>
        <dd><a href="{{ url_for('project_view', prjid=project.prjid) }}">{{ project.prjname }}</a></dd>

        <dt>Type</dt>
        <dd>{{ TYPE_NAMES.get(req.reqtype, req.reqtype) }}</dd>

        <dt>Phase</dt>
        <dd><span class="badge badge-phase badge-lg">{{ PHASE_NAMES.get(req.reqphase, req.reqphase) }}</span></dd>

        <dt>Status</dt>
        <dd><span class="badge badge-{{ req.reqstatus }} badge-lg">{{ STATUS_NAMES.get(req.reqstatus, req.reqstatus) }}</span></dd>

        <dt>Priority</dt>
        <dd>{{ req.reqpriority }}</dd>

        <dt>Code Directory</dt>
        <dd>{{ req.reqcodedir or '-' }}</dd>

        <dt>Documentation</dt>
        <dd>{{ req.reqdocpath or '-' }}</dd>

        <dt>Last Updated</dt>
        <dd>{{ req.reqtouchts }}</dd>
    </dl>
</div>

<section class="section">
    <h2>Prompt</h2>
    <div class="prompt-box">
        <pre>{{ req.reqprompt }}</pre>
    </div>
</section>

{% if req.reqerror %}
<section class="section">
    <h2>⚠️ Error</h2>
    <div class="error-box">
        <pre>{{ req.reqerror }}</pre>
    </div>
</section>
{% endif %}

<section class="section">
    <h2>
        <img src="{{ url_for('static', filename='icons/ui_editloop.png') }}" alt="">
        Update Status/Phase
    </h2>
    <form action="{{ url_for('request_update_route', reqid=req.reqid) }}" method="POST" class="form inline-form">
        <div class="form-group">
            <label for="status">Status</label>
            <select id="status" name="status">
                <option value="">-- No change --</option>
                {% for s in V2_STATUSES %}
                <option value="{{ s }}" {% if req.reqstatus == s %}selected{% endif %}>{{ STATUS_NAMES.get(s, s) }}</option>
                {% endfor %}
            </select>
        </div>

        <div class="form-group">
            <label for="phase">Phase</label>
            <select id="phase" name="phase">
                <option value="">-- No change --</option>
                {% for p in PHASES[:-1] %}
                <option value="{{ p }}" {% if req.reqphase == p %}selected{% endif %}>{{ PHASE_NAMES.get(p, p) }}</option>
                {% endfor %}
            </select>
        </div>

        <button type="submit" class="btn btn-primary">Update</button>
    </form>
</section>

<section class="section">
    <h2>
        <img src="{{ url_for('static', filename='icons/ui_pipeline.png') }}" alt="">
        Effective Infrastructure
    </h2>
    <p class="help-text">Merged from project defaults and request overrides.</p>

    {% if effective_infrastructure %}
    <table class="table table-sm">
        <thead>
            <tr>
                <th>Type</th>
                <th>Provider</th>
                <th>Value</th>
                <th>Source</th>
            </tr>
        </thead>
        <tbody>
            {% for inftype, inf in effective_infrastructure.items() %}
            <tr>
                <td>{{ inf.inftype }}</td>
                <td>{{ inf.infprovider }}</td>
                <td>{{ inf.infval or '-' }}</td>
                <td><span class="badge badge-{% if inf.source == 'request' %}info{% else %}secondary{% endif %}">{{ inf.source }}</span></td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    {% else %}
    <p class="empty-state">No infrastructure configured.</p>
    {% endif %}
</section>

<section class="section">
    <h2>Request Infrastructure Overrides</h2>
    <p class="help-text">These override project-level infrastructure settings.</p>

    {% if request_infrastructure %}
    <table class="table table-sm">
        <thead>
            <tr>
                <th>Type</th>
                <th>Provider</th>
                <th>Value</th>
                <th>Note</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            {% for inf in request_infrastructure %}
            <tr>
                <td>{{ inf.inftype }}</td>
                <td>{{ inf.infprovider }}</td>
                <td>{{ inf.infval or '-' }}</td>
                <td>{{ inf.infnote or '-' }}</td>
                <td>
                    <form action="{{ url_for('request_infrastructure_delete', rinfid=inf.rinfid) }}" method="POST" style="display:inline;">
                        <input type="hidden" name="reqid" value="{{ req.reqid }}">
                        <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Delete this override?');">Delete</button>
                    </form>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
    {% else %}
    <p class="empty-state">No request-level overrides.</p>
    {% endif %}

    <details class="add-form-toggle">
        <summary class="btn btn-sm">Add Override</summary>
        <form action="{{ url_for('request_infrastructure_add', reqid=req.reqid) }}" method="POST" class="form inline-form">
            <div class="form-row">
                <div class="form-group">
                    <label for="inftype">Type</label>
                    <select id="inftype" name="inftype" required>
                        {% for t in VALID_INFRA_TYPES %}
                        <option value="{{ t }}">{{ t }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="form-group">
                    <label for="infprovider">Provider</label>
                    <select id="infprovider" name="infprovider">
                        {% for p in VALID_PROVIDERS %}
                        <option value="{{ p }}">{{ p }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="form-group">
                    <label for="infval">Value</label>
                    <input type="text" id="infval" name="infval" placeholder="e.g., t2.micro, s3://bucket">
                </div>
                <div class="form-group">
                    <label for="infnote">Note</label>
                    <input type="text" id="infnote" name="infnote" placeholder="Optional note">
                </div>
                <div class="form-group form-actions">
                    <button type="submit" class="btn btn-primary btn-sm">Add</button>
                </div>
            </div>
        </form>
    </details>
</section>
{% endblock %}
