import type { UiMessage } from "@/stores/chat/ui-message";
import type { PendingChatUnlockKind } from "@/lib/navigation/chat_unlock_session";
import type { ChatLockType, PaymentGuidance } from "@/data/schemas/chat";
import type { PendingChatPromotion } from "@/data/storage/navigation";
import {
  DEFAULT_CHARACTER,
  DEFAULT_CHARACTER_ID,
} from "@/data/constants/character";
import type { ChatPromotionState } from "./helper/promotion";
import type { CharacterErrorCode } from "@/data/services/api";

export type ChatUpgradeReason = "insufficient_credits";

export interface ChatUnlockMessageRequest {
  displayMessageId: string;
  messageId?: string;
  kind: PendingChatUnlockKind;
  lockType?: ChatLockType;
  clientLockId?: string;
}

export interface ChatUnlockPaywallRequest
  extends ChatUnlockMessageRequest {
  paymentGuidance?: PaymentGuidance | null;
  promotion?: PendingChatPromotion;
  reason: string;
  creditBalance: number;
  requiredCredits: number;
  shortfallCredits: number;
}

export interface ChatState {
  characterId: string;
  characterErrorCode: CharacterErrorCode | null;
  emptyChatGreeting: string;
  messages: UiMessage[];
  promotion: ChatPromotionState | null;
  outgoingMessageRevision: number;
  isReplyingAI: boolean;
  pendingReplyCount: number;
  upgradePromptVisible: boolean;
  upgradeReason: ChatUpgradeReason | null;
  paymentGuidance: PaymentGuidance | null;
  canSendMessage: boolean;
  creditBalance: number;
  creditsCharged: number;
  requiredCredits: number;
  shortfallCredits: number;
  /** history 首屏可展示标志（本地快照加载完成即可进入 ready）。 */
  historyLoaded: boolean;
  /** 网络历史已返回或已明确失败；用于避免过早判定深链消息不存在。 */
  networkHistoryLoaded: boolean;
  historyTotal: number;
  historyLimit: number;
  nextHistoryOffset: number;
  isLoadingMoreHistory: boolean;
  paymentUnlockPending: boolean;
  unlockHistoryPromptVisible: boolean;
  lockedHistoryCount: number;
  unlockHistoryError: string | null;
  unlockHistoryPaymentGuidance: PaymentGuidance | null;
  unlockingMessage: ChatUnlockMessageRequest | null;
  unlockMessageError: string | null;
  unlockPaywallRequest: ChatUnlockPaywallRequest | null;
}

export function createInitialChatState(
  characterId: string,
  emptyChatGreeting: string,
): ChatState {
  return {
    characterId,
    characterErrorCode: null,
    emptyChatGreeting,
    messages: [],
    promotion: null,
    outgoingMessageRevision: 0,
    isReplyingAI: false,
    pendingReplyCount: 0,
    upgradePromptVisible: false,
    upgradeReason: null,
    canSendMessage: true,
    creditBalance: 0,
    creditsCharged: 0,
    requiredCredits: 0,
    shortfallCredits: 0,
    paymentGuidance: null,
    historyLoaded: false,
    networkHistoryLoaded: false,
    historyTotal: 0,
    historyLimit: 50,
    nextHistoryOffset: 0,
    isLoadingMoreHistory: false,
    paymentUnlockPending: false,
    unlockHistoryPromptVisible: false,
    lockedHistoryCount: 0,
    unlockHistoryError: null,
    unlockHistoryPaymentGuidance: null,
    unlockingMessage: null,
    unlockMessageError: null,
    unlockPaywallRequest: null,
  };
}

export const initialState: ChatState = createInitialChatState(
  DEFAULT_CHARACTER_ID,
  DEFAULT_CHARACTER.emptyChatGreeting,
);
