<script setup lang="ts">
definePageMeta({ ssr: false })
usePageTitle('Claims Settings')

// ── SLA Rules ─────────────────────────────────────────────────────────────────
interface SlaRule {
  lob: string
  targetDays: number
  tier1Pct: number
  tier2Pct: number
  tier3Pct: number
}

const slaRules = ref<SlaRule[]>([
  { lob: 'Auto', targetDays: 14, tier1Pct: 50, tier2Pct: 75, tier3Pct: 100 },
  { lob: 'Life', targetDays: 21, tier1Pct: 50, tier2Pct: 75, tier3Pct: 100 },
  { lob: 'General Risk', targetDays: 30, tier1Pct: 50, tier2Pct: 75, tier3Pct: 100 },
  { lob: 'Home', targetDays: 21, tier1Pct: 50, tier2Pct: 75, tier3Pct: 100 },
  { lob: 'Fianza', targetDays: 10, tier1Pct: 50, tier2Pct: 75, tier3Pct: 100 },
])

function tierDays(rule: SlaRule, pct: number) {
  return Math.round(rule.targetDays * pct / 100)
}

// ── Escalation Tiers ──────────────────────────────────────────────────────────
interface EscalationTier {
  threshold: string
  action: string
  notify: string
}

const escalationTiers = ref<EscalationTier[]>([
  { threshold: '50% of SLA', action: 'Notify handler', notify: 'Handler' },
  { threshold: '75% of SLA', action: 'Notify handler + manager', notify: 'Handler, Manager' },
  { threshold: '100% of SLA', action: 'Auto-escalate to manager', notify: 'Manager, Team Lead' },
])

// ── Required Document Gates ───────────────────────────────────────────────────
interface DocGate {
  status: string
  docTypes: string[]
}

const docGateStatuses = ['FNOL Submitted', 'Investigation', 'Documentation Pending', 'Reserved', 'Negotiation', 'Settlement']
const docTypes = ['FNOL Form', 'Police Report', 'Photos', 'Estimates', 'Medical Records', 'Proof of Loss', 'Settlement Letter']

const docGates = ref<Record<string, Record<string, boolean>>>({})

// Initialize doc gates
for (const status of docGateStatuses) {
  docGates.value[status] = {}
  for (const doc of docTypes) {
    // Defaults: FNOL always required, Photos after investigation
    if (doc === 'FNOL Form') docGates.value[status][doc] = true
    else if (doc === 'Photos' && ['Investigation', 'Documentation Pending', 'Reserved', 'Negotiation', 'Settlement'].includes(status)) docGates.value[status][doc] = true
    else if (doc === 'Estimates' && ['Reserved', 'Negotiation', 'Settlement'].includes(status)) docGates.value[status][doc] = true
    else if (doc === 'Settlement Letter' && status === 'Settlement') docGates.value[status][doc] = true
    else docGates.value[status][doc] = false
  }
}

function toggleDocGate(status: string, doc: string) {
  docGates.value[status][doc] = !docGates.value[status][doc]
}

// ── Alert Thresholds ──────────────────────────────────────────────────────────
const alertThresholds = reactive({
  reserveIncreasePct: 25,
  ageDays: 30,
  carrierNonResponseDays: 5,
  documentOverdueDays: 7,
})

// ── Form Templates ────────────────────────────────────────────────────────────
interface FormTemplate {
  id: string
  name: string
  carrier: string
  lob: string
  active: boolean
}

const formTemplates = ref<FormTemplate[]>([
  { id: 'ft-1', name: 'Informe de Accidente', carrier: 'ASSA', lob: 'Auto', active: true },
  { id: 'ft-2', name: 'Informe de Accidente', carrier: 'Qualitas', lob: 'Auto', active: true },
  { id: 'ft-3', name: 'Aviso de Pérdida', carrier: 'ASSA', lob: 'General Risk', active: true },
  { id: 'ft-4', name: 'Aviso de Pérdida', carrier: 'Mapfre', lob: 'General Risk', active: true },
  { id: 'ft-5', name: 'Reclamos Médicos', carrier: 'Pan-American Life', lob: 'Life', active: true },
  { id: 'ft-6', name: 'Reclamos Médicos', carrier: 'ASSA', lob: 'Life', active: false },
])
</script>

<template>
  <div class="cs-page">
    <!-- Header -->
    <div class="cs-header">
      <div>
        <NuxtLink to="/claims" class="cs-back-link">
          <UIcon name="i-heroicons-arrow-left" class="w-3.5 h-3.5" />
          Back to Claims
        </NuxtLink>
        <h1 class="cs-title">Claims Settings</h1>
        <p class="cs-subtitle">Configure SLA rules, escalation tiers, required documents, and alert thresholds.</p>
      </div>
      <button class="cs-save-btn">
        <UIcon name="i-heroicons-check" class="w-4 h-4" />
        Save Changes
      </button>
    </div>

    <!-- ═══ Section 1: SLA Rule Builder ═══ -->
    <div class="cs-card">
      <div class="cs-card-header">
        <UIcon name="i-heroicons-clock" class="w-5 h-5" />
        <div>
          <h2 class="cs-card-title">SLA Rule Builder</h2>
          <p class="cs-card-desc">Set target resolution days per line of business. Escalation tiers auto-compute from percentages.</p>
        </div>
      </div>
      <div class="cs-table-wrap">
        <table class="cs-table">
          <thead>
            <tr>
              <th>Line of Business</th>
              <th>Target Days</th>
              <th>Tier 1 ({{ slaRules[0]?.tier1Pct ?? 50 }}%)</th>
              <th>Tier 2 ({{ slaRules[0]?.tier2Pct ?? 75 }}%)</th>
              <th>Tier 3 ({{ slaRules[0]?.tier3Pct ?? 100 }}%)</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="rule in slaRules" :key="rule.lob">
              <td class="cs-td-bold">{{ rule.lob }}</td>
              <td>
                <input v-model.number="rule.targetDays" type="number" min="1" max="365" class="cs-input-sm" />
              </td>
              <td class="cs-td-computed">{{ tierDays(rule, rule.tier1Pct) }} days</td>
              <td class="cs-td-computed">{{ tierDays(rule, rule.tier2Pct) }} days</td>
              <td class="cs-td-computed cs-td-red">{{ tierDays(rule, rule.tier3Pct) }} days</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>

    <!-- ═══ Section 2: Escalation Tiers ═══ -->
    <div class="cs-card">
      <div class="cs-card-header">
        <UIcon name="i-heroicons-bell-alert" class="w-5 h-5" />
        <div>
          <h2 class="cs-card-title">Escalation Tiers</h2>
          <p class="cs-card-desc">Actions triggered when SLA thresholds are reached.</p>
        </div>
      </div>
      <div class="cs-escalation-list">
        <div v-for="(tier, idx) in escalationTiers" :key="idx" class="cs-escalation-row">
          <div class="cs-escalation-dot" :class="idx === 0 ? 'cs-dot-green' : idx === 1 ? 'cs-dot-amber' : 'cs-dot-red'" />
          <div class="cs-escalation-content">
            <span class="cs-escalation-threshold">{{ tier.threshold }}</span>
            <div class="cs-escalation-fields">
              <div class="cs-field-inline">
                <label class="cs-label-sm">Action</label>
                <input v-model="tier.action" type="text" class="cs-input-med" />
              </div>
              <div class="cs-field-inline">
                <label class="cs-label-sm">Notify</label>
                <input v-model="tier.notify" type="text" class="cs-input-med" />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- ═══ Section 3: Required Document Gates ═══ -->
    <div class="cs-card">
      <div class="cs-card-header">
        <UIcon name="i-heroicons-folder-open" class="w-5 h-5" />
        <div>
          <h2 class="cs-card-title">Required Document Gates</h2>
          <p class="cs-card-desc">Check which documents are required at each carrier status stage. Missing docs generate tasks automatically.</p>
        </div>
      </div>
      <div class="cs-table-wrap cs-matrix-wrap">
        <table class="cs-table cs-matrix">
          <thead>
            <tr>
              <th>Status ↓ / Doc →</th>
              <th v-for="doc in docTypes" :key="doc" class="cs-th-rotated">
                <span>{{ doc }}</span>
              </th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="status in docGateStatuses" :key="status">
              <td class="cs-td-bold">{{ status }}</td>
              <td v-for="doc in docTypes" :key="doc" class="cs-td-check" @click="toggleDocGate(status, doc)">
                <span v-if="docGates[status][doc]" class="cs-check-on">✓</span>
                <span v-else class="cs-check-off">·</span>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>

    <!-- ═══ Section 4: Alert Thresholds ═══ -->
    <div class="cs-card">
      <div class="cs-card-header">
        <UIcon name="i-heroicons-exclamation-triangle" class="w-5 h-5" />
        <div>
          <h2 class="cs-card-title">Alert Thresholds</h2>
          <p class="cs-card-desc">Configure when the system flags claims for attention.</p>
        </div>
      </div>
      <div class="cs-alert-grid">
        <div class="cs-alert-item">
          <label class="cs-label">Reserve Increase Trigger</label>
          <div class="cs-input-group">
            <input v-model.number="alertThresholds.reserveIncreasePct" type="number" min="1" max="100" class="cs-input-sm" />
            <span class="cs-input-suffix">% increase</span>
          </div>
          <p class="cs-alert-help">Alert when reserve changes by more than this percentage.</p>
        </div>
        <div class="cs-alert-item">
          <label class="cs-label">Claim Age Warning</label>
          <div class="cs-input-group">
            <input v-model.number="alertThresholds.ageDays" type="number" min="1" max="365" class="cs-input-sm" />
            <span class="cs-input-suffix">days</span>
          </div>
          <p class="cs-alert-help">Highlight claims older than this threshold.</p>
        </div>
        <div class="cs-alert-item">
          <label class="cs-label">Carrier Non-Response</label>
          <div class="cs-input-group">
            <input v-model.number="alertThresholds.carrierNonResponseDays" type="number" min="1" max="30" class="cs-input-sm" />
            <span class="cs-input-suffix">days</span>
          </div>
          <p class="cs-alert-help">Suggest escalation when carrier hasn't responded.</p>
        </div>
        <div class="cs-alert-item">
          <label class="cs-label">Document Overdue</label>
          <div class="cs-input-group">
            <input v-model.number="alertThresholds.documentOverdueDays" type="number" min="1" max="30" class="cs-input-sm" />
            <span class="cs-input-suffix">days</span>
          </div>
          <p class="cs-alert-help">Flag overdue required documents after this many days.</p>
        </div>
      </div>
    </div>

    <!-- ═══ Section 5: Carrier Form Templates ═══ -->
    <div class="cs-card">
      <div class="cs-card-header">
        <UIcon name="i-heroicons-document-duplicate" class="w-5 h-5" />
        <div>
          <h2 class="cs-card-title">Carrier Form Templates</h2>
          <p class="cs-card-desc">Manage which carrier-specific forms are available for generation. Government forms (FUD) are not managed here.</p>
        </div>
      </div>
      <div class="cs-table-wrap">
        <table class="cs-table">
          <thead>
            <tr>
              <th>Form Name</th>
              <th>Carrier</th>
              <th>LOB</th>
              <th>Active</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="ft in formTemplates" :key="ft.id">
              <td class="cs-td-bold">{{ ft.name }}</td>
              <td>{{ ft.carrier }}</td>
              <td>{{ ft.lob }}</td>
              <td>
                <button
                  class="cs-toggle"
                  :class="ft.active ? 'cs-toggle-on' : 'cs-toggle-off'"
                  @click="ft.active = !ft.active"
                >
                  <span class="cs-toggle-dot" />
                </button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* =====================================================================
   CLAIMS SETTINGS — scoped, cs- prefix
   ===================================================================== */

.cs-page {
  max-width: 64rem; margin: 0 auto;
  display: flex; flex-direction: column; gap: 24px; padding-bottom: 48px;
}

/* ── Header ── */
.cs-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.cs-back-link {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 12px; font-weight: 500; color: #8a8a86;
  text-decoration: none; margin-bottom: 8px; transition: color 150ms ease;
}
.cs-back-link:hover { color: #01696f; }
.cs-title { font-size: 22px; font-weight: 700; color: #1a1a1a; }
.cs-subtitle { font-size: 13px; color: #8a8a86; margin-top: 4px; }
.cs-save-btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 8px 18px;
  border-radius: 10px; font-size: 13px; font-weight: 600;
  background: #01696f; color: white; border: none; cursor: pointer;
}
.cs-save-btn:hover { opacity: 0.9; }

/* ── Card ── */
.cs-card {
  background: #fff; border: 1px solid rgba(0,0,0,0.06);
  border-radius: 12px; padding: 20px; box-shadow: 0 1px 3px rgba(0,0,0,0.03);
}
.cs-card-header { display: flex; align-items: flex-start; gap: 12px; margin-bottom: 16px; color: #01696f; }
.cs-card-title { font-size: 16px; font-weight: 700; color: #1a1a1a; }
.cs-card-desc { font-size: 12px; color: #8a8a86; margin-top: 2px; }

/* ── Table ── */
.cs-table-wrap { overflow-x: auto; }
.cs-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.cs-table thead th {
  padding: 8px 12px; font-size: 10px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.04em;
  color: #8a8a86; border-bottom: 1px solid rgba(0,0,0,0.06);
  text-align: left; white-space: nowrap;
}
.cs-table tbody td {
  padding: 10px 12px; border-bottom: 1px solid rgba(0,0,0,0.04);
  vertical-align: middle;
}
.cs-table tbody tr:last-child td { border-bottom: none; }
.cs-td-bold { font-weight: 600; }
.cs-td-computed { color: #5c5650; font-variant-numeric: tabular-nums; }
.cs-td-red { color: #c13838; font-weight: 600; }

/* ── Inputs ── */
.cs-input-sm {
  width: 72px; padding: 5px 8px; border: 1px solid rgba(0,0,0,0.1);
  border-radius: 6px; font-size: 13px; text-align: center;
  font-variant-numeric: tabular-nums;
}
.cs-input-sm:focus { outline: none; border-color: #01696f; }
.cs-input-med {
  flex: 1; padding: 5px 10px; border: 1px solid rgba(0,0,0,0.1);
  border-radius: 6px; font-size: 13px;
}
.cs-input-med:focus { outline: none; border-color: #01696f; }
.cs-input-group { display: flex; align-items: center; gap: 6px; }
.cs-input-suffix { font-size: 12px; color: #8a8a86; }
.cs-label { display: block; font-size: 13px; font-weight: 600; color: #1a1a1a; margin-bottom: 4px; }
.cs-label-sm { font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; color: #8a8a86; margin-bottom: 2px; }

/* ── Escalation ── */
.cs-escalation-list { display: flex; flex-direction: column; gap: 16px; }
.cs-escalation-row { display: flex; align-items: flex-start; gap: 12px; }
.cs-escalation-dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 6px; flex-shrink: 0; }
.cs-dot-green { background: #059669; }
.cs-dot-amber { background: #c27b1a; }
.cs-dot-red { background: #c13838; }
.cs-escalation-content { flex: 1; }
.cs-escalation-threshold { font-size: 14px; font-weight: 700; display: block; margin-bottom: 8px; }
.cs-escalation-fields { display: flex; gap: 12px; flex-wrap: wrap; }
.cs-field-inline { display: flex; flex-direction: column; flex: 1; min-width: 180px; }

/* ── Document Matrix ── */
.cs-matrix-wrap { max-height: 500px; }
.cs-matrix th, .cs-matrix td { text-align: center; }
.cs-matrix td:first-child, .cs-matrix th:first-child { text-align: left; }
.cs-th-rotated span {
  writing-mode: vertical-lr; transform: rotate(180deg);
  font-size: 10px; white-space: nowrap;
}
.cs-td-check { cursor: pointer; padding: 6px 8px !important; }
.cs-td-check:hover { background: rgba(1, 105, 111, 0.04); }
.cs-check-on { color: #01696f; font-weight: 700; font-size: 16px; }
.cs-check-off { color: rgba(0,0,0,0.15); font-size: 20px; }

/* ── Alert Grid ── */
.cs-alert-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.cs-alert-item { }
.cs-alert-help { font-size: 11px; color: #8a8a86; margin-top: 4px; }

/* ── Toggle ── */
.cs-toggle {
  width: 36px; height: 20px; border-radius: 10px; border: none;
  cursor: pointer; position: relative; transition: background 200ms ease;
  padding: 0;
}
.cs-toggle-on { background: #01696f; }
.cs-toggle-off { background: rgba(0,0,0,0.15); }
.cs-toggle-dot {
  display: block; width: 16px; height: 16px; border-radius: 50%;
  background: white; position: absolute; top: 2px;
  transition: left 200ms ease;
}
.cs-toggle-on .cs-toggle-dot { left: 18px; }
.cs-toggle-off .cs-toggle-dot { left: 2px; }

/* ── Responsive ── */
@media (max-width: 640px) {
  .cs-alert-grid { grid-template-columns: 1fr; }
  .cs-escalation-fields { flex-direction: column; }
}
</style>
