"use client";

import { createContext, useContext, useEffect, useState } from "react";
import type { Dispatch, ReactNode, SetStateAction } from "react";

import type { IEvent, IUser } from "@/features/crm/calendar/upstream-big-calendar/interfaces";
import type {
  TBadgeVariant,
  TCalendarView,
  TVisibleHours,
  TWorkingHours
} from "@/features/crm/calendar/upstream-big-calendar/types";

interface ICalendarContext {
  selectedDate: Date;
  setSelectedDate: (date: Date | undefined) => void;
  selectedUserId: IUser["id"] | "all";
  setSelectedUserId: (userId: IUser["id"] | "all") => void;
  selectedView: TCalendarView;
  setSelectedView: (view: TCalendarView) => void;
  badgeVariant: TBadgeVariant;
  setBadgeVariant: (variant: TBadgeVariant) => void;
  users: IUser[];
  workingHours: TWorkingHours;
  setWorkingHours: Dispatch<SetStateAction<TWorkingHours>>;
  visibleHours: TVisibleHours;
  setVisibleHours: Dispatch<SetStateAction<TVisibleHours>>;
  events: IEvent[];
  setLocalEvents: Dispatch<SetStateAction<IEvent[]>>;
  onEventChange?: (event: IEvent) => void;
}

const CalendarContext = createContext<ICalendarContext | null>(null);

const WORKING_HOURS = {
  0: { from: 0, to: 0 },
  1: { from: 8, to: 17 },
  2: { from: 8, to: 17 },
  3: { from: 8, to: 17 },
  4: { from: 8, to: 17 },
  5: { from: 8, to: 17 },
  6: { from: 8, to: 12 }
};

const VISIBLE_HOURS = { from: 7, to: 18 };

export function CalendarProvider({
  children,
  users,
  events,
  initialDate,
  initialView = "agenda",
  onEventChange
}: {
  children: ReactNode;
  users: IUser[];
  events: IEvent[];
  initialDate?: string;
  initialView?: TCalendarView;
  onEventChange?: (event: IEvent) => void;
}) {
  const [badgeVariant, setBadgeVariant] = useState<TBadgeVariant>("colored");
  const [visibleHours, setVisibleHours] = useState<TVisibleHours>(VISIBLE_HOURS);
  const [workingHours, setWorkingHours] = useState<TWorkingHours>(WORKING_HOURS);
  const [selectedDate, setSelectedDateState] = useState(() =>
    initialDate ? new Date(initialDate) : new Date()
  );
  const [selectedView, setSelectedView] = useState<TCalendarView>(initialView);
  const [selectedUserId, setSelectedUserId] = useState<IUser["id"] | "all">("all");
  const [localEvents, setLocalEvents] = useState<IEvent[]>(events);

  useEffect(() => {
    setLocalEvents(events);
  }, [events]);

  const setSelectedDate = (date: Date | undefined) => {
    if (date) setSelectedDateState(date);
  };

  return (
    <CalendarContext.Provider
      value={{
        selectedDate,
        setSelectedDate,
        selectedUserId,
        setSelectedUserId,
        selectedView,
        setSelectedView,
        badgeVariant,
        setBadgeVariant,
        users,
        workingHours,
        setWorkingHours,
        visibleHours,
        setVisibleHours,
        events: localEvents,
        setLocalEvents,
        onEventChange
      }}
    >
      {children}
    </CalendarContext.Provider>
  );
}

export function useCalendar() {
  const context = useContext(CalendarContext);
  if (!context) throw new Error("useCalendar must be used within CalendarProvider.");
  return context;
}
