// Archive view: arşivlenmiş görevler ve firmalar
const ARCIcons = window.Icons;

function ArchiveView({ state, onRestoreTask, onDeleteTask, onRestoreProject, onDeleteProject, onSelectTask, setActiveView, setActiveProject }) {

  const archivedTasks    = (state.tasks    || []).filter(t => t.archived);
  const archivedProjects = (state.projects || []).filter(p => p.archived);

  const projectById = Object.fromEntries((state.projects || []).map(p => [p.id, p]));

  const STATUS_LABELS = { todo: 'Yapılacak', doing: 'Devam Ediyor', review: 'İncelemede', done: 'Tamamlandı' };
  const PRIO_COLORS   = { high: 'var(--p-high)', medium: 'var(--p-medium)', low: 'var(--p-low)' };
  const PRIO_LABELS   = { high: 'Yüksek', medium: 'Orta', low: 'Düşük' };

  const isEmpty = archivedTasks.length === 0 && archivedProjects.length === 0;

  return (
    <div className="content">

      <div className="arc-header">
        <h2 className="arc-title">
          <ARCIcons.Archive size={20} />
          Arşiv
        </h2>
        <p className="arc-subtitle">Arşivlenen görev ve firmalar burada saklanır. Geri yükleyebilir veya kalıcı olarak silebilirsiniz.</p>
      </div>

      {isEmpty && (
        <div className="arc-empty">
          <ARCIcons.Archive size={48} strokeWidth={1} />
          <h3>Arşiv boş</h3>
          <p>Arşivlediğiniz görev veya firmalar burada görünür.</p>
        </div>
      )}

      {/* ── Arşivlenmiş Görevler ─────────────────────────────── */}
      {archivedTasks.length > 0 && (
        <div className="arc-section">
          <div className="arc-section-header">
            <ARCIcons.CheckSquare size={15} />
            <span>Görevler</span>
            <span className="arc-count">{archivedTasks.length}</span>
          </div>
          <div className="card arc-list">
            {archivedTasks.map((task, i) => {
              const proj = projectById[task.projectId];
              return (
                <div key={task.id} className={`arc-row${i < archivedTasks.length - 1 ? ' arc-row-sep' : ''}`}>
                  <div className="arc-row-main">
                    <div className="arc-row-title" onClick={() => onSelectTask?.(task.id)}>
                      {task.title}
                    </div>
                    <div className="arc-row-meta">
                      {proj && (
                        <span className="arc-chip" style={{ display: 'inline-flex', alignItems: 'center', gap: 5 }}>
                          <span className="today-proj-dot" style={{ background: proj.color }} />
                          {proj.name}
                        </span>
                      )}
                      {task.status && (
                        <span className="arc-chip">{STATUS_LABELS[task.status] || task.status}</span>
                      )}
                      {task.priority && (
                        <span className="arc-chip" style={{ color: PRIO_COLORS[task.priority] }}>
                          {PRIO_LABELS[task.priority] || task.priority}
                        </span>
                      )}
                      {task.due && (
                        <span className="arc-chip">
                          <ARCIcons.Clock size={11} /> {task.due}
                        </span>
                      )}
                    </div>
                  </div>
                  <div className="arc-row-actions">
                    <button
                      className="btn btn-ghost btn-sm"
                      onClick={() => onRestoreTask?.(task.id)}
                      title="Geri yükle"
                    >
                      <ARCIcons.ArchiveRestore size={13} /> Geri Yükle
                    </button>
                    <button
                      className="btn btn-ghost btn-sm arc-del-btn"
                      onClick={() => {
                        if (window.confirm(`"${task.title}" kalıcı olarak silinsin mi?`)) {
                          onDeleteTask?.(task.id);
                        }
                      }}
                      title="Kalıcı sil"
                    >
                      <ARCIcons.Trash size={13} />
                    </button>
                  </div>
                </div>
              );
            })}
          </div>
        </div>
      )}

      {/* ── Arşivlenmiş Firmalar ─────────────────────────────── */}
      {archivedProjects.length > 0 && (
        <div className="arc-section">
          <div className="arc-section-header">
            <ARCIcons.Folder size={15} />
            <span>Firmalar</span>
            <span className="arc-count">{archivedProjects.length}</span>
          </div>
          <div className="card arc-list">
            {archivedProjects.map((proj, i) => {
              const taskCount = (state.tasks || []).filter(t => t.projectId === proj.id).length;
              return (
                <div key={proj.id} className={`arc-row${i < archivedProjects.length - 1 ? ' arc-row-sep' : ''}`}>
                  <div className="arc-row-main">
                    <div className="arc-row-proj-badge" style={{ background: proj.color || 'var(--accent)' }}>
                      {proj.name.slice(0, 2).toUpperCase()}
                    </div>
                    <div>
                      <div className="arc-row-title">{proj.name}</div>
                      <div className="arc-row-meta">
                        <span className="arc-chip">{taskCount} görev</span>
                        {proj.description && <span className="arc-chip">{proj.description}</span>}
                      </div>
                    </div>
                  </div>
                  <div className="arc-row-actions">
                    <button
                      className="btn btn-ghost btn-sm"
                      onClick={() => {
                        onRestoreProject?.(proj.id);
                        setActiveProject?.(proj.id);
                        setActiveView?.('kanban');
                      }}
                      title="Geri yükle"
                    >
                      <ARCIcons.ArchiveRestore size={13} /> Geri Yükle
                    </button>
                    <button
                      className="btn btn-ghost btn-sm arc-del-btn"
                      onClick={() => {
                        if (window.confirm(`"${proj.name}" firması ve ${taskCount} görevi kalıcı olarak silinsin mi?`)) {
                          onDeleteProject?.(proj.id);
                        }
                      }}
                      title="Kalıcı sil"
                    >
                      <ARCIcons.Trash size={13} />
                    </button>
                  </div>
                </div>
              );
            })}
          </div>
        </div>
      )}

    </div>
  );
}

window.ArchiveView = ArchiveView;
