<template>
  <div class="village-page">
    <h1>My Village</h1>

    <div v-if="pending" class="loading">Loading your village...</div>

    <div v-else-if="error" class="error-container">
      <p v-if="error.statusCode === 401">Please log in to view your village.</p>
      <p v-else>An error occurred while fetching your village data. Please try again.</p>
    </div>

    <div v-else-if="villageData" class="village-container">
      <div class="village-grid">
        <div
          v-for="tile in villageData.tiles"
          :key="tile.id"
          class="tile"
          :class="{ selected: selectedTile && selectedTile.id === tile.id }"
          @click="selectTile(tile)"
        >
          <span class="tile-content">{{ getTileEmoji(tile) }}</span>
        </div>
      </div>

      <div v-if="selectedTile" class="action-panel">
        <h2>Tile ({{ selectedTile.x }}, {{ selectedTile.y }})</h2>
        <div class="actions-list">
          <div v-for="(action, index) in selectedTile.availableActions" :key="index" class="action-item">
            <button
              :disabled="!action.isEnabled"
              @click="handleActionClick(action)"
            >
              {{ getActionLabel(action) }}
            </button>
            <span v-if="!action.isEnabled" class="disabled-reason">{{ action.disabledReason }}</span>
          </div>
        </div>
         <button @click="selectedTile = null" class="close-panel">Close</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const { data: villageData, pending, error } = await useFetch('/api/village', {
  lazy: true,
  server: false, // Ensure this runs on the client-side
});

const selectedTile = ref(null);

const getTileEmoji = (tile) => {
  if (tile.terrainState === 'CLEARING') return '⏳';
  if (tile.object) {
    switch (tile.object.type) {
      case 'HOUSE': return '🏠';
      case 'FIELD': return '🌱';
      case 'LUMBERJACK': return '🪓';
      case 'QUARRY': return '⛏️';
      case 'WELL': return '💧';
      default: return '❓';
    }
  }
  switch (tile.terrainType) {
    case 'BLOCKED_TREE': return '🌳';
    case 'BLOCKED_STONE': return '🪨';
    case 'EMPTY': return '⬜';
    default: return '❓';
  }
};

const selectTile = (tile) => {
  selectedTile.value = tile;
};

const getActionLabel = (action) => {
  if (action.type === 'BUILD') {
    return `${action.type} ${action.buildingType} (${action.cost} coins)`;
  }
  return action.type;
};

const handleActionClick = (action) => {
  console.log('Action clicked:', action);
  // In a future task, this will dispatch the action to the backend.
};
</script>

<style scoped>
.village-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  font-family: sans-serif;
}

.loading, .error-container {
  margin-top: 50px;
  font-size: 1.2em;
  color: #555;
}

.village-container {
  display: flex;
  gap: 20px;
  margin-top: 20px;
  width: 100%;
  justify-content: center;
}

.village-grid {
  display: grid;
  grid-template-columns: repeat(5, 60px);
  grid-template-rows: repeat(7, 60px);
  gap: 4px;
  border: 2px solid #333;
  padding: 4px;
  background-color: #f0f0f0;
}

.tile {
  width: 60px;
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid #ccc;
  background-color: #fff;
  cursor: pointer;
  transition: background-color 0.2s;
}

.tile:hover {
  background-color: #e9e9e9;
}

.tile.selected {
  border: 2px solid #007bff;
  box-shadow: 0 0 10px rgba(0, 123, 255, 0.5);
}

.tile-content {
  font-size: 2em;
}

.action-panel {
  width: 300px;
  border: 1px solid #ccc;
  padding: 20px;
  background-color: #fafafa;
}

.action-panel h2 {
  margin-top: 0;
  text-align: center;
}

.actions-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.action-item {
  display: flex;
  flex-direction: column;
}

.action-item button {
  padding: 10px;
  font-size: 1em;
  cursor: pointer;
  border: 1px solid #ccc;
  background-color: #fff;
}

.action-item button:disabled {
  cursor: not-allowed;
  background-color: #eee;
  color: #999;
}

.disabled-reason {
  font-size: 0.8em;
  color: #d9534f;
  margin-top: 5px;
}

.close-panel {
  margin-top: 20px;
  width: 100%;
  padding: 10px;
}
</style>