import { DashboardFilterType } from '@/components/pages/dashboard/filter/DashboardProductionFilter.schema';
import type { ProductionStandardRepeaterFormSchemaValues } from '@/components/pages/master-data/production-standard/form/ProductionStandardForm.schema';
import type {
  UniformityFormData,
  UniformityDetail,
  VerifyUniformityResponse,
} from '@/types/api/production/uniformity';
import type { ProjectFlock } from '@/types/api/production/project-flock';

type MainUiSlice = {
  mainDrawerOpen: boolean;
  setMainDrawerOpen: (open: boolean) => void;
};

type DrawerUISlice = {
  triggerValidate: boolean;
  toggleValidate: () => void;
  subscribeValidate: (callback: () => void) => () => void;
  isValid: boolean;
  setIsValid: (v: boolean) => void;
  subscribeIsValid: (callback: (isValid: boolean) => void) => () => void;
  expandedDrawerOpen: boolean;
  setExpandedDrawerOpen: (open: boolean) => void;
  expandedDrawerContent: ReactNode | null;
  setExpandedDrawerContent: (content: ReactNode) => void;
  isNextStep: boolean;
  setIsNextStep: (v: boolean) => void;
};

type TableUISlice = {
  // State
  key: string;
  path: string;
  searchValue: string;
  previousPath: string | null;

  // Actions
  setSearchValue: (value: string) => void;
  resetSearchValue: () => void;
  setTableState: (key: string, path: string, searchValue?: string) => void;
  resetTableState: () => void;
};

// Navbar Actions Slice
type NavbarActionsSlice = {
  navbarActions: ReactNode | null;
  setNavbarActions: (actions: ReactNode) => void;
  clearNavbarActions: () => void;
};

export type UIStore = MainUiSlice &
  DrawerUISlice &
  TableUISlice &
  NavbarActionsSlice;

type ProductionStandardFormSlice = {
  formData: {
    name: string;
    project_category: string;
    details: ProductionStandardRepeaterFormSchemaValues[];
  } | null;
  editMode: boolean;
  editIndex: number | null;
  setFormData: (data: {
    name: string;
    project_category: string;
    details: ProductionStandardRepeaterFormSchemaValues[];
  }) => void;
  setEditMode: (mode: boolean, index: number | null) => void;
  addDetail: (detail: ProductionStandardRepeaterFormSchemaValues) => void;
  updateDetail: (
    index: number,
    detail: ProductionStandardRepeaterFormSchemaValues
  ) => void;
  deleteDetail: (index: number) => void;
  clearCache: () => void;
};

export type FormStore = ProductionStandardFormSlice;

export type UniformityStep = 'preview' | 'result';

export type UniformitySlice = {
  // State
  uniformityStep: UniformityStep;
  verifyUniformityResult: VerifyUniformityResponse | null;
  uniformityFormData: UniformityFormData | null;
  isSuccess: boolean;
  createdUniformity: UniformityDetail | null;

  // Actions
  setUniformityStep: (step: UniformityStep) => void;
  setVerifyUniformityResult: (result: VerifyUniformityResponse | null) => void;
  setUniformityFormData: (data: UniformityFormData | null) => void;
  setIsSuccess: (success: boolean) => void;
  setCreatedUniformity: (data: UniformityDetail | null) => void;
  resetUniformity: () => void;
};

// Dashboard Filter Slice
export type DashboardFilterSlice = {
  // State
  filterValues: DashboardFilterType;

  // Actions
  setFilterValues: (values: DashboardFilterType) => void;
  resetFilterValues: () => void;
};

export type ProjectFlockSlice = {
  // State
  isSuccess: boolean;
  createdProjectFlock: ProjectFlock | null;

  // Actions
  setIsSuccess: (success: boolean) => void;
  setCreatedProjectFlock: (data: ProjectFlock | null) => void;
  resetProjectFlock: () => void;
};

export type TableFilterStore<T = Record<string, Record<string, unknown>>> = {
  data: T;
  setData: (newData: T) => void;
  setTableData: (key: string, tableData: Record<string, unknown>) => void;
  setTableDataField: (key: string, field: string, value: unknown) => void;
  setSearchValue: (key: string, searchValue: string) => void;
};
