import { create } from "zustand";
import {
  type BackupJob,
  type BackupJobLog,
  type BackupRecord,
  type BackupSchedule,
} from "@/app/lib/api";

type BackupsState = {
  backups: BackupRecord[];
  loading: boolean;
  error: string | null;
  clients: string[];
  filterClient: string;
  createOpen: boolean;
  createClient: string;
  createLabel: string;
  createPgUri: string;
  createUseCustomUri: boolean;
  creating: boolean;
  restoreTarget: BackupRecord | null;
  restoreClient: string;
  restorePgUri: string;
  restoreUseCustomUri: boolean;
  restoring: boolean;
  deleteTarget: BackupRecord | null;
  deleting: boolean;
  schedules: BackupSchedule[];
  schedulesLoading: boolean;
  scheduleOpen: boolean;
  schedClient: string;
  schedFrequency: string;
  schedTime: string;
  schedDay: number;
  schedDayOfMonth: number;
  schedLabel: string;
  creatingSchedule: boolean;
  jobs: BackupJob[];
  jobsLoading: boolean;
  drilldownJob: BackupJob | null;
  drilldownLogs: BackupJobLog[];
  addClientOpen: boolean;
  newClientName: string;
  newClientPgUri: string;
  addingClient: boolean;
};

type BackupsActions = {
  setState: (patch: Partial<BackupsState>) => void;
  resetCreateForm: (clients: string[]) => void;
  prepareRestoreForm: (backup: BackupRecord) => void;
  prepareScheduleForm: (clients: string[]) => void;
  closeDrilldown: () => void;
  resetAddClientForm: () => void;
};

const initialState: BackupsState = {
  backups: [],
  loading: false,
  error: null,
  clients: [],
  filterClient: "",
  createOpen: false,
  createClient: "",
  createLabel: "",
  createPgUri: "",
  createUseCustomUri: false,
  creating: false,
  restoreTarget: null,
  restoreClient: "",
  restorePgUri: "",
  restoreUseCustomUri: false,
  restoring: false,
  deleteTarget: null,
  deleting: false,
  schedules: [],
  schedulesLoading: false,
  scheduleOpen: false,
  schedClient: "",
  schedFrequency: "daily",
  schedTime: "02:00",
  schedDay: 1,
  schedDayOfMonth: 1,
  schedLabel: "",
  creatingSchedule: false,
  jobs: [],
  jobsLoading: false,
  drilldownJob: null,
  drilldownLogs: [],
  addClientOpen: false,
  newClientName: "",
  newClientPgUri: "",
  addingClient: false,
};

export const useBackupsStore = create<BackupsState & BackupsActions>()(
  (set) => ({
    ...initialState,

    setState: (patch) => set((state) => ({ ...state, ...patch })),

    resetCreateForm: (clients) =>
      set({
        createClient: clients[0] ?? "",
        createLabel: "",
        createPgUri: "",
        createUseCustomUri: false,
        createOpen: true,
      }),

    prepareRestoreForm: (backup) =>
      set({
        restoreTarget: backup,
        restoreClient: backup.client_name,
        restorePgUri: "",
        restoreUseCustomUri: false,
      }),

    prepareScheduleForm: (clients) =>
      set({
        schedClient: clients[0] ?? "",
        schedFrequency: "daily",
        schedTime: "02:00",
        schedDay: 1,
        schedDayOfMonth: 1,
        schedLabel: "",
        scheduleOpen: true,
      }),

    closeDrilldown: () => set({ drilldownJob: null, drilldownLogs: [] }),

    resetAddClientForm: () =>
      set({
        addClientOpen: false,
        newClientName: "",
        newClientPgUri: "",
      }),
  }),
);
