export const PLAN_STATUSES = ['등록', '작업중', '작업완료', '릴리즈완료', '완료'] as const;
export const PLAN_FILTER_STATUSES = ['all', 'in-progress', 'done', 'error'] as const;

export type PlanStatus = (typeof PLAN_STATUSES)[number];
export type PlanFilterStatus = (typeof PLAN_FILTER_STATUSES)[number];
export type PlanAutomationType = string;
export type PlanActionType =
  | 'start-work'
  | 'complete-development'
  | 'retry-branch'
  | 'retry-work'
  | 'retry-merge'
  | 'cancel-release'
  | 'request-main-merge';

export type PlanIssueHistory = {
  id: number;
  planItemId: number;
  issueTag: string;
  message: string;
  actionNote: string | null;
  resolved: boolean;
  resolvedAt: string | null;
  createdAt: string;
};

export type PlanActionHistory = {
  id: number;
  planItemId: number;
  actionType: string;
  note: string;
  createdAt: string;
};

export type PlanSourceWorkHistory = {
  id: number;
  planItemId: number;
  summary: string;
  branchName: string;
  commitHash: string | null;
  previewUrl: string | null;
  changedFiles: string[];
  commandLog: string | null;
  diffText: string | null;
  sourceFiles: PlanSourceFileSnapshot[];
  createdAt: string;
};

export type PlanSourceFileSnapshotStatus = 'added' | 'modified' | 'deleted' | 'renamed' | 'binary' | 'unknown';

export type PlanSourceFileSnapshot = {
  path: string;
  previousPath: string | null;
  status: PlanSourceFileSnapshotStatus;
  language: string;
  content: string;
};

export const PLAN_RELEASE_REVIEW_STATUSES = ['pending', 'reviewing', 'approved', 'changes-requested'] as const;

export type PlanReleaseReviewStatus = (typeof PLAN_RELEASE_REVIEW_STATUSES)[number];

export type PlanReleaseReviewMetadata = {
  summary?: string;
  pageSelectionIds?: string[];
  checkedPageSelectionIds?: string[];
  docIds?: string[];
  componentIds?: string[];
  widgetIds?: string[];
};

export type PlanReleaseReview = {
  id: number | null;
  planItemId: number;
  status: PlanReleaseReviewStatus;
  reviewNote: string;
  checkedByClientId: string | null;
  checkedByNickname: string | null;
  checkedAt: string | null;
  metadata: PlanReleaseReviewMetadata;
  createdAt: string | null;
  updatedAt: string | null;
};

export type PlanReleaseReviewBoardItem = {
  planItem: PlanItem;
  review: PlanReleaseReview;
  latestSourceWork: PlanSourceWorkHistory | null;
};

export type PlanAutomationUsageSnapshot = {
  tokenTotals: {
    total: number;
    input: number;
    output: number;
    cached: number;
    reasoning: number;
  };
  totalTokens: number;
  retryCount: number;
  sourceWorkCount: number;
  processingStartedAt: string | null;
  processingEndedAt: string | null;
  processingEndedAtSource: string | null;
  processingDurationSeconds: number | null;
};

export type PlanItem = {
  id: number;
  workId: string;
  note: string;
  automationType: PlanAutomationType;
  automationBehaviorType?: string;
  automationContextIds: string[];
  releaseReviewNote: string;
  noteMasked?: boolean;
  status: PlanStatus;
  jangsingProcessingRequired: boolean;
  autoDeployToMain: boolean;
  suppressWebPush: boolean;
  repeatRequestEnabled: boolean;
  repeatIntervalMinutes: number;
  assignedBranch: string | null;
  releaseTarget: string | null;
  workerStatus: string | null;
  lastError: string | null;
  issueTags: string[];
  hasOpenIssues: boolean;
  startedAt: string | null;
  completedAt: string | null;
  mergedAt: string | null;
  usageSnapshot: PlanAutomationUsageSnapshot | null;
  createdAt: string;
  updatedAt: string;
};

export type PlanDraft = {
  id: number | null;
  workId: string;
  note: string;
  automationType: PlanAutomationType;
  automationContextIds: string[];
  status: PlanStatus;
  jangsingProcessingRequired: boolean;
  autoDeployToMain: boolean;
  suppressWebPush: boolean;
  repeatRequestEnabled: boolean;
  repeatIntervalMinutes: number;
};
