import { TableColumn } from "../types/common";
import { Etat, IAvenant } from "../types/circuit";
import { SelectedDaysType } from "../types/usager";
import dayjs from "dayjs";
import { WeeklyScheduleType } from "../sections/usager/components/usager-form/avenants-horaires/types/formik-types";
import { AmendmentCircuit } from "../types/ammendment-circuit";

export const _AVENANT_TYPE_MODIFICATIONS = [
  "Changement d'adresse",
  "Modification de planning",
  "Changement de chauffeur",
  "Modification de tarif",
];

export const _AVENANT_TYPE_GENERATION = [
  "automatique",
  "manuel",
];

export const _avenants =  (isSecondaryTable: boolean) => [...Array(50)].map((_, index) => {
    const startDate = new Date(2002 + Math.floor(index / 12), index % 12, 15);
    const endDate = new Date(startDate.getFullYear() + 2, Math.floor(Math.random() * 12), 1);
    const typeOption = isSecondaryTable ?_AVENANT_TYPE_MODIFICATIONS : _AVENANT_TYPE_GENERATION
  return {
    id: (index + 1).toString(),
    dateApplication: startDate.toISOString().split('T')[0],
    etat: index % 3 === 0 ? Etat.Controle : Etat.NonControle,
    codeAvenant: `AVE-2024-${String.fromCharCode(65 + (index % 26))}${String.fromCharCode(65 + (Math.floor(index / 26)))}`,
    codeCircuit: `CIR-2024-${String.fromCharCode(65 + (index % 26))}${String.fromCharCode(65 + (Math.floor(index / 26)))}`,
    intitule: `Avenant ${index + 1}`,
    objetAvenant: ["Adresse Domicile", "Horaires", "Véhicule", "Tarification"][index % 4],
    type: typeOption[index % typeOption.length],
    dateFin: endDate.toISOString().split('T')[0],
    etablissement: ['Les Champs', 'La Colline', 'Le Rivage', 'La Forêt'][index % 4],
    chauffeur: Math.random() > 0.3 ? `Chauffeur ${index + 1}` : "Non Affecté",
    departement: `Région ${['Est', 'Ouest', 'Nord', 'Sud'][index % 4]}`,
    clientDO: `DO ${['Nord', 'Sud', 'Est', 'Ouest'][index % 4]}`,
    isActive: Math.random() < 0.6,
    objet: "Transport Scolaire",
  };
});


export const DEFAULT_AVENANT_CIRCUIT_TABLE_HEAD: TableColumn[] = [
  { id: 'id', label: 'N° Avenant', type: 'text', align: 'center' },
  { id: 'startDate', label: 'A partir du', type: 'date', align: 'center' },
  { id: 'circuitState', label: 'État', type: 'status', align: 'left' },
  { id: 'code', label: 'Code', type: 'text', align: 'left' },
  { id: 'title', label: 'Intitulé', type: 'text', align: 'left' },
  { id: 'amendmentObject', label: 'Objet de l\'avenant', type: 'text', align: 'left' },
  { id: 'calculationType', label: 'Type', type: 'text', align: 'left' },
  { id: 'circuitEndDate', label: 'Fin de circuit', type: 'date', align: 'center' },
  { id: 'establishment', label: 'Établissement', type: 'text', align: 'left' },
  { id: 'driver', label: 'Chauffeur', type: 'text', align: 'left' },
  { id: 'department', label: 'Département', type: 'text', align: 'left' },
  { id: 'doClient', label: 'Client DO', type: 'text', align: 'left' },
  { id: 'createdAt', label: 'Créé le', type: 'date', align: 'center' },
  { id: 'isActive', label: 'Active', type: 'boolean',align: 'center' },
  { id: '', label: 'Action' },
];

export const DEFAULT_AVENANT_TABLE_HEAD: TableColumn[] = [
  { id: 'id', label: 'N° Avenant', type: 'text', align: 'center' },
  { id: 'startDate', label: 'Date de création', type: 'date', align: 'center' },
  { id: 'circuitEndDate', label: 'Fin de circuit', type: 'date', align: 'center' , isNotSortable: true },
  { id: 'circuitState', label: 'État', type: 'status', align: 'left' },
  { id: 'code', label: 'Code Avenant', type: 'text', align: 'left' },
  { id: 'title', label: 'Intitulé', type: 'text', align: 'left' },
  { id: 'doClient', label: 'Client DO', type: '', align: 'left' , isNotSortable: true},
  { id: 'driver', label: 'Chauffeur', type: '', align: 'left' , isNotSortable: true },
  { id: 'department', label: 'Département', type: 'text', align: 'left' , isNotSortable: true },
  { id: 'amendmentObject', label: 'Objet de l\'avenant', type: 'text', align: 'left' },
  { id: 'actionType', label: 'Type', type: 'text', align: 'left' },
  { id: 'createdAt', label: 'Créé le', type: 'date', align: 'center' },
  { id: 'isActive', label: 'Active', type: 'boolean',align: 'center' },
  { id: '', label: 'Action' },
];

export const INITIAL_AVENANT_DATA: AmendmentCircuit = {
  id: '',
  startDate: '',
  circuitState: '',
  code: '',
  title: '',
  amendmentObject: '',
  calculationType: '',
  circuitEndDate: '',
  establishment: '',
  driver: '',
  department: '',
  doClient: '',
  pickupAddress: '',
  priceGrid: '',
  colorCode: '',
  observation: '',
  passengerScheduleWeeks: [],
  actionType: '',
  isActive: true,
  objet: '',
  typeCalcul: '',
};

export const MOCKED_SCHEDULE_DATA: WeeklyScheduleType = {
  pair: {
    lundi: {
      morning: { aller: dayjs().hour(8).minute(0) },
      evening: { retour: dayjs().hour(18).minute(0) }
    },    mardi:  {
      morning: { aller: dayjs().hour(8).minute(0) },
      evening: { retour: dayjs().hour(18).minute(0) }
    },
    mercredi: {
      morning: { aller: dayjs().hour(8).minute(0) },
      evening: { retour: dayjs().hour(18).minute(0) }
    },
    jeudi: {
      morning: { aller: dayjs().hour(8).minute(0) },
      evening: { retour: dayjs().hour(18).minute(0) }
    },
    vendredi: {
      morning: { aller: dayjs().hour(8).minute(0) },
      evening: { retour: dayjs().hour(18).minute(0) }
    },
    samedi: {
      morning: { aller: dayjs().hour(8).minute(0) },
      evening: { retour: dayjs().hour(18).minute(0) }
    },
    dimanche: {
      morning: { aller: dayjs().hour(8).minute(0) },
      evening: { retour: dayjs().hour(18).minute(0) }
    },
  },
  impair: {
    lundi: {
      morning: { aller: dayjs().hour(8).minute(0) },
      evening: { retour: dayjs().hour(18).minute(0) }
    },
    mardi: {
      morning: { aller: dayjs().hour(8).minute(0) },
      evening: { retour: dayjs().hour(18).minute(0) }
    },
    mercredi: {
      morning: { aller: dayjs().hour(8).minute(0) },
      evening: { retour: dayjs().hour(18).minute(0) }
    },
    jeudi: {
      morning: { aller: dayjs().hour(8).minute(0) },
      evening: { retour: dayjs().hour(18).minute(0) }
    },
    vendredi: {
      morning: { aller: dayjs().hour(8).minute(0) },
      evening: { retour: dayjs().hour(18).minute(0) }
    },
    samedi: {
      morning: { aller: dayjs().hour(8).minute(0) },
      evening: { retour: dayjs().hour(18).minute(0) }
    },
    dimanche: {
      morning: { aller: dayjs().hour(8).minute(0) },
      evening: { retour: dayjs().hour(18).minute(0) }
    },
  },
}
