{% extends "base.html" %}
{% block title %}{% if scenario %}Edit {{ scenario.name }}{% else %}New Scenario{% endif %} · CalMiner{% endblock %}

{% block head_extra %}
  <link rel="stylesheet" href="/static/css/scenarios.css" />
{% endblock %}

{% block content %}
  <nav class="breadcrumb">
    <a href="{{ url_for('projects.project_list_page') }}">Projects</a>
    <a href="{{ url_for('projects.view_project', project_id=project.id) }}">{{ project.name }}</a>
    {% if scenario %}
      <span aria-current="page">Edit Scenario</span>
    {% else %}
      <span aria-current="page">New Scenario</span>
    {% endif %}
  </nav>

  {% set error = error | default(None) %}
  {% set error_field = error_field | default(None) %}
  {% set currency_error = error if error_field == 'currency' else None %}
  {% set name_error = error if error_field == 'name' else None %}

  {% if error and not error_field %}
    <div class="alert alert-error">{{ error }}</div>
  {% endif %}

  <form class="form scenario-form" method="post" action="{{ form_action }}">
    <header class="page-header">
      <div>
        <h1>{% if scenario %}Edit Scenario{% else %}Create Scenario{% endif %}</h1>
        <p class="text-muted">Scenarios inherit pricing defaults from <strong>{{ project.name }}</strong>.</p>
      </div>
      <div class="header-actions">
  <a class="btn btn--secondary" href="{{ cancel_url }}">Cancel</a>
  <button class="btn btn--primary" type="submit">Save Scenario</button>
      </div>
    </header>

    <section class="card scenario-context-card">
      <h2>Project Context</h2>
      <p class="field-help">Defaults below come from project pricing. Leave optional fields blank to reuse shared assumptions.</p>
      <dl class="definition-list">
        <div>
          <dt>Project</dt>
          <dd><a href="{{ url_for('projects.view_project', project_id=project.id) }}">{{ project.name }}</a></dd>
        </div>
        <div>
          <dt>Operation Type</dt>
          <dd>{{ project.operation_type.value.replace('_', ' ') | title }}</dd>
        </div>
        <div>
          <dt>Default Currency</dt>
          <dd>{{ default_currency or 'Not configured' }}</dd>
        </div>
      </dl>
      <ul class="scenario-defaults">
        <li>
          <strong>Status Guidance</strong>
          <span>Draft scenarios remain internal; switch to Active once this represents your baseline, and Archive when retiring assumptions.</span>
        </li>
        <li>
          <strong>Baseline Reminder</strong>
          <span>Keep a single Active scenario to serve as the default baseline when launching profitability or planner workflows.</span>
        </li>
        <li>
          <strong>Currency Tip</strong>
          <span>If you leave currency empty, CalMiner applies the project default shown above.</span>
        </li>
      </ul>
    </section>

    <section class="card">
      <h2>Scenario Overview</h2>
      <div class="form-grid">
        <div class="form-group{% if name_error %} form-group--error{% endif %}">
          <label for="name">Name</label>
          <input id="name" name="name" type="text" required value="{{ scenario.name if scenario else '' }}" {% if name_error %}aria-invalid="true"{% endif %} />
          {% if name_error %}
            <p class="field-error">{{ name_error }}</p>
          {% else %}
            <p class="field-help">Name must be unique within {{ project.name }}.</p>
          {% endif %}
        </div>

        <div class="form-group">
          <label for="status">Status</label>
          <select id="status" name="status">
            {% for value, label in scenario_statuses %}
              <option value="{{ value }}" {% if scenario and scenario.status.value == value %}selected{% endif %}>{{ label }}</option>
            {% endfor %}
          </select>
          <p class="field-help">Use Draft while iterating, set Active for your go-to baseline, and Archive to keep historical context.</p>
        </div>

        <div class="form-group{% if currency_error %} form-group--error{% endif %}">
          <label for="currency">Currency</label>
          {% set currency_prefill = scenario.currency if scenario and scenario.currency else default_currency %}
          <input id="currency" name="currency" type="text" maxlength="3" value="{{ currency_prefill or '' }}" placeholder="{{ default_currency or '' }}" {% if currency_error %}aria-invalid="true"{% endif %} />
          {% if currency_error %}
            <p class="field-error">{{ currency_error }}</p>
          {% else %}
            <p class="field-help">Use a three-letter ISO code (e.g., USD). Defaults to {{ default_currency or 'the project currency' }}.</p>
          {% endif %}
        </div>

        <div class="form-group">
          <label for="primary_resource">Primary Resource</label>
          <select id="primary_resource" name="primary_resource">
            <option value="">—</option>
            {% for value, label in resource_types %}
              <option value="{{ value }}" {% if scenario and scenario.primary_resource and scenario.primary_resource.value == value %}selected{% endif %}>{{ label }}</option>
            {% endfor %}
          </select>
          <p class="field-help">Optional. Helps planners prioritise inputs tied to this commodity.</p>
        </div>
      </div>
    </section>

    <section class="card">
      <h2>Assumptions & Timeline</h2>
      <div class="form-grid">
        <div class="form-group">
          <label for="start_date">Start Date</label>
          <input id="start_date" name="start_date" type="date" value="{{ scenario.start_date if scenario else '' }}" />
          <p class="field-help">Optional. Use to align calculations with anticipated project kickoff.</p>
        </div>
        <div class="form-group">
          <label for="end_date">End Date</label>
          <input id="end_date" name="end_date" type="date" value="{{ scenario.end_date if scenario else '' }}" />
          <p class="field-help">Optional. Leave blank for open-ended scenarios.</p>
        </div>
        <div class="form-group">
          <label for="discount_rate">Discount Rate (%)</label>
          <input id="discount_rate" name="discount_rate" type="number" step="0.01" value="{{ scenario.discount_rate if scenario else '' }}" />
          <p class="field-help">Leave empty to reuse the project default during profitability calculations.</p>
        </div>
      </div>

      <div class="form-group">
        <label for="description">Description</label>
        <textarea id="description" name="description" rows="5" placeholder="Describe the key drivers or differences for this scenario.">{{ scenario.description if scenario else '' }}</textarea>
        <p class="field-help">Summarise what distinguishes this scenario for collaborators and future audits.</p>
      </div>
    </section>

    <div class="form-actions">
  <a class="btn btn--secondary" href="{{ cancel_url }}">Cancel</a>
  <button class="btn btn--primary" type="submit">Save Scenario</button>
    </div>
  </form>
{% endblock %}
