export type TokenStatus = "missing" | "valid" | "expired" | "revoked";

export type TaskStatus = "pending" | "running" | "completed" | "failed";

export type TaskExecutorMode = "api" | "browser" | "manual";

export type PinDraftStatus = "draft" | "pending_review" | "scheduled" | "published" | "failed";

export type CommercialPlan = "starter" | "growth" | "pro" | "enterprise";

export type LicenseStatus = "trial" | "active" | "expired" | "suspended";

export type LicenseHeartbeatStatus = "not_configured" | "ok" | "grace" | "failed" | "expired";

export type LicenseConfig = {
  licenseId: string;
  customerName: string;
  plan: CommercialPlan;
  status: LicenseStatus;
  purchaseType?: "trial" | "subscription" | "one_time";
  issuedAt: string;
  expiresAt: string;
  supportExpiresAt?: string;
  machineId?: string;
  seats: number;
  pinterestAccounts: number;
  verificationStatus?: "unverified" | "valid" | "invalid";
  signature?: string;
  verifiedAt?: string;
  remoteServerUrl?: string;
  remoteLicenseKey?: string;
  remoteStatus?: LicenseStatus;
  heartbeatStatus?: LicenseHeartbeatStatus;
  heartbeatError?: string;
  lastHeartbeatAt?: string;
  nextHeartbeatAt?: string;
  leaseExpiresAt?: string;
  graceHours?: number;
  remoteUsageSeconds?: number;
};

export type PinterestAccount = {
  id: string;
  accountName: string;
  businessAccount: boolean;
  scopes: string[];
  tokenStatus: TokenStatus;
  connectedAt?: string;
  expiresAt?: string;
  updatedAt: string;
};

export type PinterestOAuthState = {
  state?: string;
  scopes: string[];
  redirectUri?: string;
  createdAt?: string;
};

export type PinterestOAuthToken = {
  accessToken?: string;
  refreshToken?: string;
  tokenType?: string;
  scope?: string;
  expiresAt?: string;
  refreshExpiresAt?: string;
  updatedAt?: string;
};

export type PinterestBoard = {
  id: string;
  apiBoardId?: string;
  name: string;
  description?: string;
  privacy: "PUBLIC" | "PROTECTED" | "SECRET";
  pinCount?: number;
  createdAt: string;
  updatedAt: string;
};

export type PinterestPinDraft = {
  id: string;
  boardId: string;
  apiPinId?: string;
  title: string;
  description: string;
  link?: string;
  mediaUrl?: string;
  mediaType: "image" | "video";
  keywords: string[];
  status: PinDraftStatus;
  scheduledAt?: string;
  evidence?: string;
  createdAt: string;
  updatedAt: string;
};

export type OperatorPlanStatus = "draft" | "running" | "completed" | "failed";

export type OperatorPlanItemStatus = "pending" | "running" | "completed" | "failed" | "skipped";

export type OperatorPlanItemType =
  | "check_health"
  | "research_trends"
  | "create_pin_drafts"
  | "publish_pin"
  | "read_analytics"
  | "catalog_check"
  | "ads_report"
  | "crm_follow_up"
  | "report";

export type OperatorPlanItem = {
  id: string;
  planId: string;
  type: OperatorPlanItemType;
  brief: string;
  status: OperatorPlanItemStatus;
  scheduledAt?: string;
  taskId?: string;
  evidence?: string;
  resultSummary?: string;
};

export type OperatorPlan = {
  id: string;
  date: string;
  goal: string;
  status: OperatorPlanStatus;
  summary: string;
  createdFromCommand: string;
  items: OperatorPlanItem[];
  createdAt: string;
  updatedAt: string;
};

export type PinterestTrendResearch = {
  id: string;
  seedKeyword: string;
  market: string;
  terms: Array<{
    keyword: string;
    category?: string;
    intent: "idea" | "shopping" | "seasonal" | "brand" | "unknown";
    score: number;
    reason: string;
  }>;
  summary: string;
  createdAt: string;
};

export type PinterestLead = {
  id: string;
  sourceType: "pin_analytics" | "lead_ad" | "website" | "manual_research" | "catalog";
  sourceId?: string;
  name?: string;
  profileUrl?: string;
  email?: string;
  website?: string;
  company?: string;
  keyword?: string;
  pinId?: string;
  boardId?: string;
  score: number;
  intent: "buyer" | "partner" | "creator" | "merchant" | "unknown";
  recommendedAction: "crm_follow_up" | "email" | "review_profile" | "ignore";
  status: "new" | "reviewing" | "contacted" | "converted" | "ignored";
  evidence: string;
  createdAt: string;
  updatedAt: string;
};

export type PinterestAutomationTask = {
  id: string;
  type:
    | "check_health"
    | "create_board"
    | "create_pin_draft"
    | "publish_pin"
    | "read_pin_analytics"
    | "research_trends"
    | "catalog_check"
    | "ads_report"
    | "browser_snapshot";
  executorMode: TaskExecutorMode;
  status: TaskStatus;
  targetUrl?: string;
  payload: Record<string, unknown>;
  result?: string;
  evidence?: TaskEvidence[];
  createdAt: string;
  updatedAt: string;
};

export type TaskEvidence = {
  id: string;
  label: string;
  path?: string;
  url?: string;
  capturedAt: string;
};

export type AuditEvent = {
  id: string;
  type: "account" | "content" | "trend" | "lead" | "task" | "system";
  actor: "user" | "openclaw" | "api" | "browser" | "system";
  title: string;
  detail: string;
  status: "pending" | "completed" | "failed" | "review";
  targetUrl?: string;
  evidence?: string;
  createdAt: string;
};

export type KnowledgeDocument = {
  id: string;
  title: string;
  source: "manual" | "upload" | "url";
  content: string;
  createdAt: string;
  updatedAt: string;
};

export type RiskLimits = {
  dailyTrendResearches: number;
  dailyPinPublishes: number;
  dailyBoardCreates: number;
  dailyApiWrites: number;
  requireApprovalForPublish: boolean;
  requireApprovalForFollow: boolean;
  requireApprovalForComment: boolean;
  requireApprovalForMessage: boolean;
};
