import type { components } from "../../../packages/openapi/generated/api";

export type Schemas = components["schemas"];
export type Resident = Schemas["Resident"];
export type ResidentSearchReq = Schemas["ResidentSearchReq"];
export type PagedResidents = Schemas["PagedResidents"];
export type Transaction = Schemas["Transaction"];
export type MoveInReq = Schemas["MoveInReq"];
export type MoveOutReq = Schemas["MoveOutReq"];
export type OfficialTxReq = Schemas["OfficialTxReq"];
export type ApprovalReq = Schemas["ApprovalReq"];
export type CancelReq = Schemas["CancelReq"];
export type JuminCodeReq = Schemas["JuminCodeReq"];
export type MyNumberReq = Schemas["MyNumberReq"];
export type CodeIssueResult = Schemas["CodeIssueResult"];
export type CertificateReq = Schemas["CertificateReq"];
export type CertificateIssue = Schemas["CertificateIssue"];
export type VerifyResult = Schemas["VerifyResult"];
export type ReportReq = Schemas["ReportReq"];
export type EucQueryReq = Schemas["EucQueryReq"];
export type AsyncJob = Schemas["AsyncJob"];
export type AuditLog = Schemas["AuditLog"];
export type Me = Schemas["Me"];
export type Restriction = Schemas["Restriction"];
export type ForeignerInfo = Schemas["ForeignerInfo"];
export type ForeignerUpdateResult = Schemas["ForeignerUpdateResult"];
export type ForeignerExpiryReportReq = Schemas["ForeignerExpiryReportReq"];
export type ForeignerExpiryJob = Schemas["ForeignerExpiryJob"];
export type AliasName = Schemas["AliasName"];

export type ViewId = "overview" | "search" | "resident" | "move" | "official" | "certificate" | "fee" | "restriction" | "reports" | "admin" | "notify" | "conveni" | "alias" | "special" | "alerts" | "eucdesign" | "batch" | "link";

// 証明手数料の算定（標準仕様書: 証明種別×通数 + 郵送料）
export type CertFeeReq = { certType: string; copies: number; postal: boolean };
export type CertFeeResult = {
  certType: string;
  unitFee: number;
  copies: number;
  subtotal: number;
  postalFee: number;
  total: number;
};

export type LinkApplication = {
  id: string;
  source: string;
  externalId: string | null;
  receiptNumber: string | null;
  procedureType: string;
  applicant: { residentId: string | null; name: string };
  status: "RECEIVED" | "PROCESSING" | "COMPLETED" | "REJECTED";
  receivedAt: string;
  updatedAt: string;
};

// システム情報（運用・監視）
export type SystemInfo = {
  node: string;
  platform: string;
  pid: number;
  uptimeSeconds: number;
  serverTime: string;
  dataCounts: Record<string, number>;
};

// お知らせ（運用周知）
export type Announcement = {
  id: string;
  title: string;
  body: string;
  level: "info" | "warning" | "critical";
  status: string;
  createdBy?: string;
  createdAt: string;
};

// ダッシュボード（SCR-002）
export type Overview = {
  residents: { total: number; active: number; foreigners: number; specialPermanent: number; restricted: number };
  transactions: { total: number; pendingApproval: number };
  certificates: number;
  conveniRequests: number;
  notifyRegistrations: number;
  notifications: number;
  eucTemplates: number;
  batchJobs: number;
  alerts: number;
};

// バッチ管理（標準仕様書 9）
export type BatchType = { type: string; name: string; description: string };
export type BatchJob = {
  jobId: string;
  type: string;
  name: string;
  status: string;
  startedAt: string;
  finishedAt: string;
  processed: number;
  details: Record<string, number>;
  executedBy?: string;
};

// EUC設計（SCR-A01）
export type EucTemplate = {
  id: string;
  name: string;
  domain: string;
  outputFields: string[];
  includeMyNumber: boolean;
  requiresSecondApproval: boolean;
  createdBy?: string;
  createdAt?: string;
};

// エラー・アラート設定 / アクセスログ分析（SCR-A04）
export type AlertRules = {
  nightAccessEnabled: boolean;
  nightStartHour: number;
  nightEndHour: number;
  bulkSearchEnabled: boolean;
  bulkSearchThreshold: number;
};
export type AlertItem = {
  type: "NIGHT_ACCESS" | "BULK_SEARCH";
  severity: string;
  userId: string;
  message: string;
  occurredAt?: string;
  count?: number;
};

// 特別永住者証明書（SCR-802）
export type SpecialPermanentCert = {
  certNumber: string;
  issuedDate: string;
  expiryDate: string;
  note?: string;
};

// 通称・旧氏管理（SCR-103）
export type AliasRecord = {
  aliasId: string;
  kind: "ALIAS" | "FORMER_FAMILY";
  valueKanji: string;
  valueKana?: string;
  validFrom: string;
  validTo: string | null;
};

export type SearchCriteria = {
  name: string;
  address: string;
  foreignerOnly: boolean;
  includeRemoved: boolean;
};

export type EucAsyncJob = AsyncJob & { requiresSecondApproval?: boolean };

// 本人通知制度（標準仕様書 8.1 / SCR-801）
export type NotifyRegistration = {
  registrationId: string;
  residentId: string;
  registeredAt: string;
  expiresAt: string;
  status: "ACTIVE" | "INACTIVE";
  endedAt?: string;
  note?: string;
};

export type HonninNotification = {
  notificationId: string;
  residentId: string;
  issueId: string;
  formId: string;
  requesterType: string;
  certifiedAt: string;
  notifiedAt: string;
  channel: string;
  status: string;
};

// コンビニ交付（標準仕様書 第5章 / SCR-507）
export type ConveniRequest = {
  conveniId: string;
  residentId: string;
  formId: string;
  storeCode: string;
  cardSerial?: string;
  requestedAt: string;
  status: "ISSUED" | "REFUSED" | "NOT_FOUND" | "PENDING";
  issueId: string | null;
  reason: string | null;
};

export type ConveniStatus = {
  partner: string;
  linkState: string;
  serviceHours: string;
  checkedAt: string;
  totals: { total: number; issued: number; refused: number; pending: number };
};
