<!-- #region TaskResultPanel [C:2] [TYPE Component] [SEMANTICS task, result, summary, status, plugin] -->
<!-- @BRIEF Displays formatted task result summary with status badges and result details. -->
<!-- @LAYER UI -->
<!-- @RELATION DEPENDS_ON -> [EXT:frontend:i18n] -->
<!-- @UX_STATE Empty -> No task selected, placeholder shown. -->
<!-- @UX_STATE Loaded -> Task result displayed with status color coding. -->
<script lang="ts">
  import { t } from '$lib/i18n/index.svelte.js';
  let { task = null } = $props();

  const result = $derived(task?.result || null);
  const pluginId = $derived(task?.plugin_id || '');

  function statusColor(status) {
    switch (status) {
      case 'PASS':
      case 'SUCCESS':
        return 'bg-green-100 text-green-700';
      case 'WARN':
      case 'PARTIAL_SUCCESS':
        return 'bg-yellow-100 text-yellow-700';
      case 'FAIL':
      case 'FAILED':
        return 'bg-red-100 text-red-700';
      default:
        return 'bg-slate-100 text-slate-700';
    }
  }
</script>

{#if !task}
  <div class="rounded-lg border border-dashed border-slate-200 bg-slate-50 p-6 text-sm text-slate-500">
    {$t.tasks?.result_select_task}
  </div>
{:else if !result}
  <div class="rounded-lg border border-slate-200 bg-white p-4">
    <p class="text-sm text-slate-700">{$t.tasks?.result_not_structured}</p>
  </div>
{:else if pluginId === 'llm_dashboard_validation'}
  <div class="space-y-4 rounded-lg border border-slate-200 bg-white p-4">
    <div class="flex items-center justify-between">
      <h3 class="text-sm font-semibold text-slate-900">{$t.tasks?.result_llm_validation}</h3>
      <span class={`rounded-full px-2 py-1 text-xs font-semibold ${statusColor(result.status)}`}>{result.status || 'UNKNOWN'}</span>
    </div>
    <p class="text-sm text-slate-700">{result.summary || $t.tasks?.result_no_summary}</p>
    {#if result.issues?.length}
      <div>
        <p class="mb-2 text-xs font-semibold uppercase tracking-wide text-slate-500">{$t.tasks?.issues} ({result.issues.length})</p>
        <ul class="space-y-2">
          {#each result.issues as issue}
            <li class="rounded-md border border-slate-200 bg-slate-50 p-2 text-sm">
              <div class="flex items-center gap-2">
                <span class={`rounded px-2 py-0.5 text-xs font-semibold ${statusColor(issue.severity)}`}>{issue.severity}</span>
                <span class="text-slate-700">{issue.message}</span>
              </div>
              {#if issue.location}
                <p class="mt-1 text-xs text-slate-500">{$t.tasks?.location}: {issue.location}</p>
              {/if}
            </li>
          {/each}
        </ul>
      </div>
    {/if}
  </div>
{:else if pluginId === 'superset-backup'}
  <div class="space-y-4 rounded-lg border border-slate-200 bg-white p-4">
    <div class="flex items-center justify-between">
      <h3 class="text-sm font-semibold text-slate-900">{$t.tasks?.backup_result}</h3>
      <span class={`rounded-full px-2 py-1 text-xs font-semibold ${statusColor(result.status)}`}>{result.status || 'UNKNOWN'}</span>
    </div>
    <div class="grid grid-cols-2 gap-2 text-sm text-slate-700">
      <p>{$t.dashboard?.environment}: {result.environment || $t.common?.not_available}</p>
      <p>{$t.tasks?.total}: {result.total_dashboards ?? 0}</p>
      <p>{$t.tasks?.successful}: {result.backed_up_dashboards ?? 0}</p>
      <p>{$t.tasks?.failed}: {result.failed_dashboards ?? 0}</p>
    </div>
    {#if result.failures?.length}
      <div>
        <p class="mb-2 text-xs font-semibold uppercase tracking-wide text-slate-500">{$t.tasks?.errors}</p>
        <ul class="space-y-2">
          {#each result.failures as failure}
            <li class="rounded-md border border-destructive-light bg-destructive-light p-2 text-sm text-red-700">
              {failure.title || failure.id}: {failure.error}
            </li>
          {/each}
        </ul>
      </div>
    {/if}
  </div>
{:else if pluginId === 'superset-migration'}
  <div class="space-y-4 rounded-lg border border-slate-200 bg-white p-4">
    <div class="flex items-center justify-between">
      <h3 class="text-sm font-semibold text-slate-900">{$t.tasks?.migration_result}</h3>
      <span class={`rounded-full px-2 py-1 text-xs font-semibold ${statusColor(result.status)}`}>{result.status || 'UNKNOWN'}</span>
    </div>
    <div class="grid grid-cols-2 gap-2 text-sm text-slate-700">
      <p>{$t.migration?.source_env}: {result.source_environment || $t.common?.not_available}</p>
      <p>{$t.migration?.target_env}: {result.target_environment || $t.common?.not_available}</p>
      <p>{$t.tasks?.selected}: {result.selected_dashboards ?? 0}</p>
      <p>{$t.tasks?.successful}: {result.migrated_dashboards?.length ?? 0}</p>
      <p>{$t.tasks?.with_errors}: {result.failed_dashboards?.length ?? 0}</p>
      <p>{$t.tasks?.mappings}: {result.mapping_count ?? 0}</p>
    </div>
    {#if result.failed_dashboards?.length}
      <div>
        <p class="mb-2 text-xs font-semibold uppercase tracking-wide text-slate-500">{$t.tasks?.migration_errors}</p>
        <ul class="space-y-2">
          {#each result.failed_dashboards as failed}
            <li class="rounded-md border border-destructive-light bg-destructive-light p-2 text-sm text-red-700">
              {failed.title || failed.id}: {failed.error}
            </li>
          {/each}
        </ul>
      </div>
    {/if}
  </div>
{:else}
  <div class="rounded-lg border border-slate-200 bg-white p-4">
    <pre class="overflow-auto rounded bg-slate-50 p-3 text-xs text-slate-700">{JSON.stringify(result, null, 2)}</pre>
  </div>
{/if}

<!-- #endregion TaskResultPanel -->
