import { create } from "zustand";
import { type WorkspaceSettings } from "@/app/lib/store";

type FeedbackCategory = "issue" | "idea";

type WorkspacePageState = {
  name: string;
  slug: string;
  region: string;
  fiscalMonth: string;
  logoInitials: string;
  logoColor: string;
  deleteOpen: boolean;
  deleteConfirm: string;
  feedbackOpen: boolean;
  feedbackCategory: FeedbackCategory;
  feedbackTitle: string;
  feedbackMessage: string;
  feedbackEmail: string;
  feedbackSubmitting: boolean;
  orgName: string;
  orgType: string;
  orgPlan: string;
  orgRequestedBy: string;
  orgNotes: string;
  orgSubmitting: boolean;
  projectOrganization: string;
  projectName: string;
  projectRegion: string;
  projectRequester: string;
  projectPassword: string;
  projectEnableDataApi: boolean;
  projectEnableAutoRls: boolean;
  projectSubmitting: boolean;
};

type WorkspacePageActions = {
  setState: (patch: Partial<WorkspacePageState>) => void;
  initializeFromWorkspace: (workspace: WorkspaceSettings) => void;
  setNameAndSlug: (name: string) => void;
  clearFeedbackForm: () => void;
  clearOrganizationForm: () => void;
  clearProjectForm: () => void;
};

const slugify = (val: string) =>
  val
    .toLowerCase()
    .replace(/[^a-z0-9-]/g, "-")
    .replace(/-+/g, "-")
    .replace(/^-|-$/g, "");

const initialState: WorkspacePageState = {
  name: "",
  slug: "",
  region: "",
  fiscalMonth: "January",
  logoInitials: "",
  logoColor: "#6366f1",
  deleteOpen: false,
  deleteConfirm: "",
  feedbackOpen: false,
  feedbackCategory: "issue",
  feedbackTitle: "",
  feedbackMessage: "",
  feedbackEmail: "",
  feedbackSubmitting: false,
  orgName: "",
  orgType: "Personal",
  orgPlan: "Free - $0/month",
  orgRequestedBy: "",
  orgNotes: "",
  orgSubmitting: false,
  projectOrganization: "",
  projectName: "",
  projectRegion: "Europe",
  projectRequester: "",
  projectPassword: "",
  projectEnableDataApi: true,
  projectEnableAutoRls: false,
  projectSubmitting: false,
};

export const useWorkspacePageStore = create<
  WorkspacePageState & WorkspacePageActions
>()((set) => ({
  ...initialState,

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

  initializeFromWorkspace: (workspace) =>
    set((state) => ({
      ...state,
      name: workspace.name,
      slug: workspace.slug,
      region: workspace.region,
      fiscalMonth: workspace.fiscalYearStart,
      logoInitials: workspace.logoInitials,
      logoColor: workspace.logoColor,
      projectOrganization:
        state.projectOrganization || workspace.name || state.projectOrganization,
      feedbackEmail:
        state.feedbackEmail || state.orgRequestedBy || state.projectRequester || "",
    })),

  setNameAndSlug: (name) =>
    set((state) => ({
      ...state,
      name,
      slug: slugify(name),
    })),

  clearFeedbackForm: () =>
    set((state) => ({
      feedbackCategory: "issue" as FeedbackCategory,
      feedbackTitle: "",
      feedbackMessage: "",
      feedbackEmail: state.orgRequestedBy || state.projectRequester || "",
      feedbackOpen: false,
    })),

  clearOrganizationForm: () =>
    set({
      orgName: "",
      orgRequestedBy: "",
      orgNotes: "",
    }),

  clearProjectForm: () =>
    set({
      projectName: "",
      projectRequester: "",
      projectPassword: "",
    }),
}));
