<template>
  <div class="workspace-header">
    <div class="selected-tournament-strip">
      <div class="selected-tournament-main">
        <span class="selected-tournament-label">{{ $t('tournaments.selectedTournament') }}</span>
        <strong>{{ tournamentName || selectionDisplay || '–' }}</strong>
      </div>
      <div class="selected-tournament-meta">
        <span>{{ tournamentDateLabel }}</span>
        <span>{{ modeLabel }}</span>
        <span>{{ classCount }} {{ $t('tournaments.classes') }}</span>
        <span>{{ participantCount }} {{ $t('tournaments.participants') }}</span>
      </div>
    </div>

    <div class="tournament-tabs-panel">
      <div class="tournament-tabs">
        <button @click="$emit('set-active-tab', 'config')" :class="['tab-button', { active: activeTab === 'config' }]">
          {{ $t('tournaments.tabConfig') }}
        </button>
        <button @click="$emit('set-active-tab', 'participants')" :class="['tab-button', { active: activeTab === 'participants' }]">
          {{ $t('tournaments.tabParticipants') }}
          <span class="tab-badge">{{ participantCount }}</span>
        </button>
        <button v-if="isGroupTournament" @click="$emit('set-active-tab', 'groups')" :class="['tab-button', { active: activeTab === 'groups' }]">
          {{ $t('tournaments.tabGroups') }}
          <span class="tab-badge">{{ groupCount }}</span>
        </button>
        <button @click="openResultsMatches" :class="['tab-button', { active: activeTab === 'results' && resultsSubTab === 'matches' }]">
          {{ $t('tournaments.tabResults') }}
          <span class="tab-badge">{{ matchCount }}</span>
        </button>
        <button @click="openResultsPlacements" :class="['tab-button', { active: activeTab === 'results' && resultsSubTab === 'placements' }]">
          {{ $t('tournaments.tabPlacements') }}
        </button>
      </div>
    </div>

    <div v-if="highlightStatusChips.length > 0" class="tournament-status-strip">
      <div v-for="status in highlightStatusChips" :key="status.key" :class="['tournament-status-chip', `status-${status.tone}`]">
        <button v-if="status.action" type="button" class="tournament-status-main" @click="$emit('navigate-status', status.action)">{{ status.label }}</button>
        <span v-else class="tournament-status-main tournament-status-main-static">{{ status.label }}</span>
        <button v-if="status.quickAction" type="button" class="tournament-status-action" @click="$emit('quick-action', status.quickAction)">{{ status.quickAction.label }}</button>
      </div>
    </div>

    <div v-if="workspaceProblems.length > 0" class="workspace-problems">
      <div class="workspace-problems-header">
        <strong>{{ $t('tournaments.workspaceProblemsTitle', { count: workspaceProblems.length }) }}</strong>
      </div>
      <div class="workspace-problems-list">
        <div v-for="problem in workspaceProblems" :key="problem.key" class="workspace-problem-item">
          <div class="workspace-problem-copy">
            <strong class="workspace-problem-title">{{ problem.title }}</strong>
            <span class="workspace-problem-description">{{ problem.description }}</span>
          </div>
          <button
            type="button"
            class="workspace-problem-action"
            @click="problem.quickAction ? $emit('quick-action', problem.quickAction) : $emit('navigate-status', problem.action)"
          >
            {{ problem.actionLabel }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TournamentWorkspaceHeader',
  props: {
    tournamentName: { type: String, default: '' },
    selectionDisplay: { type: String, default: '' },
    tournamentDateLabel: { type: String, default: '' },
    modeLabel: { type: String, default: '' },
    classCount: { type: Number, default: 0 },
    participantCount: { type: Number, default: 0 },
    groupCount: { type: Number, default: 0 },
    matchCount: { type: Number, default: 0 },
    workspaceStatusChips: { type: Array, required: true },
    workspaceProblems: { type: Array, required: true },
    activeTab: { type: String, required: true },
    resultsSubTab: { type: String, required: true },
    isGroupTournament: { type: Boolean, default: false }
  },
  emits: ['navigate-status', 'quick-action', 'set-active-tab', 'set-results-sub-tab'],
  computed: {
    highlightStatusChips() {
      return (this.workspaceStatusChips || []).filter(status => status.tone === 'warning' || status.quickAction);
    }
  },
  methods: {
    openResultsMatches() {
      this.$emit('set-active-tab', 'results');
      this.$emit('set-results-sub-tab', 'matches');
    },
    openResultsPlacements() {
      this.$emit('set-active-tab', 'results');
      this.$emit('set-results-sub-tab', 'placements');
    }
  }
};
</script>

<style scoped>
.workspace-header {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.selected-tournament-strip {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: flex-start;
}

.selected-tournament-main {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.selected-tournament-main strong {
  color: #111827;
  font-size: 1.2rem;
}

.selected-tournament-label {
  color: #6b7280;
  font-size: 0.82rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.selected-tournament-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  justify-content: flex-end;
}

.selected-tournament-meta span {
  background: #f3f4f6;
  border: 1px solid rgba(47, 122, 95, 0.12);
  border-radius: 999px;
  padding: 0.35rem 0.7rem;
  color: #4b5563;
  font-size: 0.9rem;
  font-weight: 600;
}

.tournament-tabs-panel {
  padding: 0.45rem;
  border-radius: 18px;
  background: #ffffff;
  border: 1px solid rgba(47, 122, 95, 0.14);
  box-shadow: 0 10px 24px rgba(24, 70, 54, 0.08);
}

.tournament-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
}

.tab-button {
  padding: 0.9rem 1.15rem;
  background: #f8faf9;
  border: 1px solid rgba(47, 122, 95, 0.08);
  border-radius: 14px;
  display: inline-flex;
  align-items: center;
  gap: 0.7rem;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 700;
  color: #4b5563;
  transition: all 0.18s ease;
}

.tab-button:hover {
  transform: translateY(-1px);
  background: var(--primary-light);
  border-color: rgba(47, 122, 95, 0.14);
  color: var(--primary-strong);
}

.tab-button.active {
  background: linear-gradient(135deg, rgba(24, 70, 54, 0.96), rgba(47, 122, 95, 0.94));
  border-color: rgba(24, 70, 54, 0.9);
  color: #ffffff;
  box-shadow: 0 10px 24px rgba(24, 70, 54, 0.18);
}

.tab-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.65rem;
  padding: 0.14rem 0.5rem;
  border-radius: 999px;
  background: rgba(47, 122, 95, 0.12);
  color: var(--primary-strong);
  font-size: 0.82rem;
  font-weight: 800;
}

.tab-button.active .tab-badge {
  background: rgba(255, 255, 255, 0.2);
  color: #ffffff;
}

.tournament-status-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
}

.tournament-status-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.45rem 0.75rem;
  border-radius: 999px;
  font-size: 0.88rem;
  font-weight: 700;
}

.tournament-status-main {
  border: none;
  background: transparent;
  color: inherit;
  font: inherit;
  padding: 0;
  cursor: pointer;
}

.tournament-status-main-static {
  cursor: default;
}

.tournament-status-action {
  border: none;
  border-radius: 999px;
  padding: 0.2rem 0.55rem;
  background: rgba(255, 255, 255, 0.82);
  color: inherit;
  font-size: 0.76rem;
  font-weight: 800;
  cursor: pointer;
}

.tournament-status-chip.status-success {
  background: #dcfce7;
  color: #166534;
}

.tournament-status-chip.status-warning {
  background: #fef3c7;
  color: #92400e;
}

.tournament-status-chip.status-info {
  background: #dbeafe;
  color: #1d4ed8;
}

.workspace-problems {
  padding: 0.9rem 1rem;
  border: 1px solid #fed7aa;
  border-radius: 16px;
  background: #fff7ed;
}

.workspace-problems-header {
  margin-bottom: 0.75rem;
  color: #9a3412;
}

.workspace-problems-list {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.workspace-problem-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 0.85rem;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.72);
}

.workspace-problem-copy {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  min-width: 0;
}

.workspace-problem-title {
  color: #7c2d12;
}

.workspace-problem-description {
  color: #9a3412;
  font-size: 0.88rem;
}

.workspace-problem-action {
  flex-shrink: 0;
  padding: 0.45rem 0.8rem;
  border: 1px solid #fdba74;
  border-radius: 999px;
  background: #fff;
  color: #9a3412;
  font-weight: 700;
  cursor: pointer;
}

@media (max-width: 900px) {
  .selected-tournament-strip {
    flex-direction: column;
  }

  .selected-tournament-meta {
    justify-content: flex-start;
  }

  .workspace-problem-item {
    flex-direction: column;
    align-items: flex-start;
  }
}
</style>
