/**
 * Pinia Store for Trial Balance (Balanță de Verificare)
 */
import { defineStore } from "pinia";
import { ref, computed } from "vue";
import api from "@reports/services/api";

export const useTrialBalanceStore = defineStore("trialBalance", () => {
  // State
  const trialBalanceData = ref([]);
  const isLoading = ref(false);
  const error = ref(null);

  // Totaluri din TOATE înregistrările filtrate (nu doar pagina curentă)
  const totals = ref({
    total_sold_precedent_debit: 0,
    total_sold_precedent_credit: 0,
    total_rulaj_lunar_debit: 0,
    total_rulaj_lunar_credit: 0,
    total_sold_final_debit: 0,
    total_sold_final_credit: 0,
  });

  const filters = ref({
    luna: new Date().getMonth() + 1, // Current month (1-12)
    an: new Date().getFullYear(), // Current year
    cont: "",
    denumire: "",
  });

  const pagination = ref({
    currentPage: 1,
    pageSize: 50,
    totalItems: 0,
    totalPages: 0,
  });

  const sorting = ref({
    sortBy: "CONT",
    sortOrder: "asc",
  });

  // Getters
  const hasData = computed(() => trialBalanceData.value.length > 0);

  const currentPeriod = computed(() => {
    return {
      luna: filters.value.luna,
      an: filters.value.an,
    };
  });

  // Actions
  const fetchTrialBalance = async (companyCode) => {
    if (!companyCode) {
      error.value = "Company code is required";
      return { success: false, error: error.value };
    }

    isLoading.value = true;
    error.value = null;

    try {
      const params = {
        company: companyCode,
        luna: filters.value.luna,
        an: filters.value.an,
        page: pagination.value.currentPage,
        page_size: pagination.value.pageSize,
        sort_by: sorting.value.sortBy,
        sort_order: sorting.value.sortOrder,
      };

      // Add optional filters
      if (filters.value.cont) {
        params.cont_filter = filters.value.cont;
      }
      if (filters.value.denumire) {
        params.denumire_filter = filters.value.denumire;
      }

      const response = await api.get("/trial-balance/", { params });

      if (response.data.success) {
        trialBalanceData.value = response.data.data.items || [];

        // Update pagination
        const paginationData = response.data.data.pagination;
        pagination.value = {
          currentPage: paginationData.current_page,
          pageSize: paginationData.page_size,
          totalItems: paginationData.total_items,
          totalPages: paginationData.total_pages,
        };

        // Store totals from ALL filtered records (not just current page)
        if (response.data.data.totals) {
          totals.value = response.data.data.totals;
        }

        return { success: true };
      } else {
        throw new Error("Invalid response format");
      }
    } catch (err) {
      error.value =
        err.response?.data?.detail || "Failed to load trial balance data";
      console.error("Failed to load trial balance:", err);
      return { success: false, error: error.value };
    } finally {
      isLoading.value = false;
    }
  };

  const applyFilters = async (newFilters, companyCode) => {
    filters.value = { ...filters.value, ...newFilters };
    pagination.value.currentPage = 1; // Reset to first page when filtering
    await fetchTrialBalance(companyCode);
  };

  const clearFilters = async (companyCode) => {
    filters.value = {
      luna: new Date().getMonth() + 1,
      an: new Date().getFullYear(),
      cont: "",
      denumire: "",
    };
    pagination.value.currentPage = 1;
    await fetchTrialBalance(companyCode);
  };

  const changePage = async (page, companyCode) => {
    pagination.value.currentPage = page;
    await fetchTrialBalance(companyCode);
  };

  const changePageSize = async (pageSize, companyCode) => {
    pagination.value.pageSize = pageSize;
    pagination.value.currentPage = 1; // Reset to first page
    await fetchTrialBalance(companyCode);
  };

  const sort = async (sortBy, sortOrder, companyCode) => {
    sorting.value = { sortBy, sortOrder };
    pagination.value.currentPage = 1; // Reset to first page when sorting
    await fetchTrialBalance(companyCode);
  };

  const changePeriod = async (luna, an, companyCode) => {
    filters.value.luna = luna;
    filters.value.an = an;
    pagination.value.currentPage = 1;
    await fetchTrialBalance(companyCode);
  };

  const clearError = () => {
    error.value = null;
  };

  const reset = () => {
    trialBalanceData.value = [];
    isLoading.value = false;
    error.value = null;
    totals.value = {
      total_sold_precedent_debit: 0,
      total_sold_precedent_credit: 0,
      total_rulaj_lunar_debit: 0,
      total_rulaj_lunar_credit: 0,
      total_sold_final_debit: 0,
      total_sold_final_credit: 0,
    };
    filters.value = {
      luna: new Date().getMonth() + 1,
      an: new Date().getFullYear(),
      cont: "",
      denumire: "",
    };
    pagination.value = {
      currentPage: 1,
      pageSize: 50,
      totalItems: 0,
      totalPages: 0,
    };
    sorting.value = {
      sortBy: "CONT",
      sortOrder: "asc",
    };
  };

  return {
    // State
    trialBalanceData,
    isLoading,
    error,
    totals,
    filters,
    pagination,
    sorting,

    // Getters
    hasData,
    currentPeriod,

    // Actions
    fetchTrialBalance,
    applyFilters,
    clearFilters,
    changePage,
    changePageSize,
    sort,
    changePeriod,
    clearError,
    reset,
  };
});
