<template>
  <q-card flat class="holidays-card card-ring">
    <div class="flex justify-between items-center no-wrap q-mb-sm">
      <div class="flex items-center q-gutter-x-sm">
        <q-icon name="mdi-calendar-star-outline" color="dark" size="sm" />
        <span class="text-subtitle1">Feriados do Mês</span>
      </div>
      <q-btn
        icon="mdi-plus"
        color="primary"
        style="width: 40px; height: 40px"
        @click="openDialog"
      />
    </div>

    <q-separator />

    <div
      v-if="currentMonthHolidays.length === 0"
      class="text-caption text-grey-5 text-center q-mt-md"
    >
      Sem feriados para este mês.
    </div>

    <q-list v-else class="q-mt-md" dense>
      <template v-for="(holiday, index) in currentMonthHolidays" :key="holiday.id">
        <q-item class="q-px-none person-item cursor-pointer" clickable @click="openEdit(holiday)">
          <q-item-section avatar style="min-width: 36px">
            <div class="day-badge">{{ holiday.day }}</div>
          </q-item-section>
          <q-item-section>
            <q-item-label class="text-body2">{{ holiday.description }}</q-item-label>
            <q-item-label caption>{{ typeLabel(holiday.type) }}</q-item-label>
          </q-item-section>
          <q-item-section side>
            <q-icon name="mdi-pencil-outline" color="grey-5" size="xs" />
          </q-item-section>
        </q-item>
        <q-separator v-if="index < currentMonthHolidays.length - 1" />
      </template>
    </q-list>
  </q-card>
</template>

<script setup>
import { ref, computed, onMounted } from "vue";
import { useQuasar } from "quasar";
import HolidaysDialog from "src/pages/dashboard/components/HolidaysDialog.vue";
import HolidayEditDialog from "src/pages/dashboard/components/HolidayEditDialog.vue";
import { getBaseHolidays } from "src/api/holiday";

const $q = useQuasar();

const now = new Date();
const holidays = ref([]);

function toFrontend(h) {
  const [year, month, day] = h.holiday_date.split("-").map(Number);
  return { id: h.id, day, month, year, description: h.description, type: h.type };
}

async function loadHolidays() {
  try {
    const items = await getBaseHolidays();
    holidays.value = items.map(toFrontend);
  } catch {
    holidays.value = [];
  }
}

onMounted(loadHolidays);

const currentMonthHolidays = computed(() =>
  holidays.value
    .filter((h) => h.month === now.getMonth() + 1 && h.year === now.getFullYear())
    .sort((a, b) => a.day - b.day),
);

function typeLabel(type) {
  return type === "facultativo" ? "Ponto Facultativo" : "Feriado";
}

function openDialog() {
  $q.dialog({
    component: HolidaysDialog,
    componentProps: { initialHolidays: holidays.value },
  }).onOk((updated) => {
    holidays.value = updated;
  });
}

function openEdit(holiday) {
  $q.dialog({
    component: HolidayEditDialog,
    componentProps: { holiday },
  }).onOk(({ action, holiday: updated, id }) => {
    if (action === "update") {
      const idx = holidays.value.findIndex((h) => h.id === updated.id);
      if (idx !== -1) holidays.value[idx] = updated;
    } else if (action === "delete") {
      holidays.value = holidays.value.filter((h) => h.id !== id);
    }
  });
}
</script>

<style scoped>
.holidays-card {
  border-radius: 12px;
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  max-height: 370px;
  overflow: hidden;
}

.person-item {
  padding-top: 6px;
  padding-bottom: 6px;
}

.day-badge {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background-color: #e64a19;
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
</style>
