import type {
  BuilderFunnelState,
  BuilderScreen,
} from "@/lib/admin/builder/types";
import type { ScreenDefinition, NavigationRuleDefinition } from "@/lib/funnel/types";

export interface BuilderState extends BuilderFunnelState {
  selectedScreenId: string | null;
  isDirty: boolean;
}

export type BuilderAction =
  | { type: "set-meta"; payload: Partial<BuilderFunnelState["meta"]> }
  | { type: "set-default-texts"; payload: Partial<BuilderFunnelState["defaultTexts"]> }
  | { type: "add-screen"; payload?: { template?: ScreenDefinition["template"] } & Partial<BuilderScreen> }
  | { type: "insert-screen"; payload: { template?: ScreenDefinition["template"]; atIndex: number } & Partial<BuilderScreen> }
  | { type: "remove-screen"; payload: { screenId: string } }
  | { type: "update-screen"; payload: { screenId: string; screen: Partial<BuilderScreen> } }
  | { type: "reorder-screens"; payload: { fromIndex: number; toIndex: number } }
  | { type: "set-selected-screen"; payload: { screenId: string | null } }
  | { type: "set-screens"; payload: BuilderScreen[] }
  | {
      type: "update-navigation";
      payload: {
        screenId: string;
        navigation: {
          defaultNextScreenId?: string | null;
          rules?: NavigationRuleDefinition[];
          isEndScreen?: boolean;
          onBackScreenId?: string | null;
        };
      };
    }
  | { type: "reset"; payload?: BuilderState };

export interface BuilderProviderProps {
  children: React.ReactNode;
  initialState?: BuilderState;
}
