<script>
/** Import required modules */
import { ref, watch } from "vue";
import { SessionUser } from "@/components/session/token/SessionUser.vue";

/** Generate a unique ID (This is used to make sure the data is updated when the department ID or the selected date changes) */
const GENERATE_UNIQUE_ID = () => {
  return Math.random().toString(36).substr(2, 9);
};

/** Dates */
// Get the current date
const DATE_TODAY = () => {
  const date = new Date();
  return date.toISOString().split('T')[0];
};

// Get the date of yesterday
const DATE_YESTERDAY = () => {
  const date = new Date();
  date.setDate(date.getDate() - 1);
  return date.toISOString().split('T')[0];
};

/** Shortcuts */
const date_shortcuts = ref({
  // Today
  today: {
    label: 'I dag',
    value: 'today',
    date: DATE_TODAY(),
  },
  // Yesterday
  yesterday: {
    label: 'I går',
    value: 'yesterday',
    date: DATE_YESTERDAY(),
  },
});

/** Dynamic variables */
// Current selected date, this date is used to fetch the data for the selected date.
const selected_date = ref(DATE_TODAY()); // Set the selected date to today, by default.
// Selected department ID
const selected_department_id = ref(0); // Set the selected department ID to 0, by default.
// COUNT: The number of transactions for the selected date. (A.K.A. the number of orders)
const count_transactions = ref(null);
// COUNT: The number of products sold for the selected date. (Total quantity of products sold)
const count_products_sold = ref(null);
// COUNT: The earnings for the selected date. (Total earnings for the selected date)
const count_earnings = ref(null);
// Unique ID ( This is used to make sure the data is updated when the department ID or the selected date changes )
const uniqueId = ref(GENERATE_UNIQUE_ID());

/** Get the transactions, and products sold for the selected date */
const updateCountTransactions = async () => {
  // If the department ID is not set, return
  if (!parseInt(selected_department_id.value) > 0) {
    return;
  }
  await SessionUser.objects.department_daily_reports.functions.getTransactionCount(selected_department_id.value, selected_date.value)
    .then(response => {
      count_transactions.value = response.data.data.quantity;
      count_products_sold.value = response.data.data.products;
      count_earnings.value = response.data.data.earnings;
    });
};

/** Update all, this function is called when the department ID or the selected date changes */
const onChange = () => {
  // Generate a new unique ID ( This is done, to make sure all data is updated. - Even in other, independent components. This is done to prevent any issues with the data not being updated )
  uniqueId.value = GENERATE_UNIQUE_ID();
  // Update the transactions and products sold (count)
  updateCountTransactions();
};


/**
 * Watch the unique ID, and call the callback function when the unique ID changes.
 * This is used to make sure the data is updated when the department ID or the selected date changes.
 * @param callback The callback function to call when the unique ID changes.
 */
const onChangeCall = ( callback ) => {
  watch(uniqueId, (newValue) => {
    callback();
  });
};

/** Select a date */
const selectDate = (date) => {
  // Validate the date format Y-m-d
  const date_regex = /^\d{4}-\d{2}-\d{2}$/;
  if (!date.match(date_regex)) {
    console.error('Invalid date format');
    return;
  }
  // Set the selected date
  selected_date.value = date;
};

/** Select a department */
const selectDepartment = (department_id) => {
  // Validate the department ID
  if (!parseInt(department_id) > 0) {
    console.error('Invalid department ID');
    return;
  }
  // Set the selected department ID
  selected_department_id.value = parseInt(department_id);
};

/** Watch the selected date */
watch(selected_date, (new_date) => {
  onChange();
});

/** Watch the selected department ID */
watch(selected_department_id, (new_department_id) => {
  onChange();
});

/** Export the variables */
export { date_shortcuts, selected_date, selectDate, count_transactions, count_products_sold, count_earnings, selected_department_id, selectDepartment, onChangeCall };


</script>