import type { Elder } from "@/modules/elders/types";

export const ROLE_IDS = [
  "platform_admin",
  "platform_operator",
  "platform_auditor",
  "platform_ops",
  "org_admin",
  "manager",
  "caregiver",
  "viewer",
  "family",
  "resident",
] as const;
export type RoleId = (typeof ROLE_IDS)[number];

export const ROLE_LABELS: Record<RoleId, string> = {
  platform_admin: "平台超管",
  platform_operator: "平台运营",
  platform_auditor: "平台审计",
  platform_ops: "平台运维",
  org_admin: "机构管理员",
  manager: "运营主管",
  caregiver: "照护人员",
  viewer: "普通用户",
  family: "亲属",
  resident: "本人",
};

export const PERMISSIONS = [
  "platform:manage",
  "organization:read",
  "organization:manage",
  "account:read",
  "account:manage",
  "role:read",
  "role:manage",
  "permission:read",
  "audit:read",
  "incident:read",
  "incident:manage",
  "care:read",
  "care:manage",
  "health:read",
  "health:manage",
  "device:read",
  "device:manage",
  "notice:read",
  "notice:manage",
  "alert:read",
  "alert:manage",
  "family:read",
  "family:manage",
  "facility:read",
  "facility:manage",
  "admission:read",
  "admission:manage",
  "elder:read",
  "elder:create",
  "elder:update",
  "elder:delete",
  "ai:read",
  "ai:manage",
  "knowledge:read",
  "knowledge:manage",
] as const;

export type Permission = (typeof PERMISSIONS)[number];

export type RoleDefinition = {
  id: string;
  key: RoleId | string;
  label: string;
  description: string;
  scope: "platform" | "organization";
  organizationId?: string;
  isSystem: boolean;
  isEnabled: boolean;
  permissions: Permission[];
};

export type AccountStatus = "active" | "disabled" | "pending";

export type OrganizationStatus = "active" | "disabled";

export type Organization = {
  id: string;
  name: string;
  slug: string;
  status: OrganizationStatus;
  registrationEnabled: boolean;
  oidcEnabled: boolean;
  oidcIssuerUrl: string;
  oidcClientId: string;
  oidcHasClientSecret: boolean;
  oidcScopes: string;
  oidcRedirectUri: string;
  oidcAvatarClaim: string;
  oidcAutoProvision: boolean;
  createdAt: string;
  updatedAt: string;
};

export type SystemSettings = {
  id: string;
  registrationEnabled: boolean;
  oidcEnabled: boolean;
  oidcIssuerUrl: string;
  oidcClientId: string;
  oidcHasClientSecret: boolean;
  oidcScopes: string;
  oidcRedirectUri: string;
  oidcAvatarClaim: string;
  oidcAutoProvision: boolean;
  updatedAt: string;
};

export type MembershipStatus = "active" | "disabled" | "pending";

export type Membership = {
  id: string;
  accountId: string;
  organizationId: string;
  roleId: string;
  roleKey: string;
  roleLabel: string;
  status: MembershipStatus;
  createdAt: string;
  updatedAt: string;
};

export type JoinRequestStatus = "pending" | "approved" | "rejected";

export type JoinRequest = {
  id: string;
  accountId: string;
  accountName: string;
  accountEmail: string;
  organizationId: string;
  organizationName: string;
  status: JoinRequestStatus;
  reason: string;
  reviewedByAccountId?: string;
  reviewedAt?: string;
  createdAt: string;
};

export type OrganizationInvitationStatus = "active" | "accepted" | "revoked" | "expired";

export type OrganizationInvitation = {
  id: string;
  organizationId: string;
  organizationName: string;
  roleId: string;
  roleLabel: string;
  email: string;
  token: string;
  status: OrganizationInvitationStatus;
  createdByAccountId?: string;
  acceptedByAccountId?: string;
  acceptedAt?: string;
  expiresAt: string;
  maxUses: number;
  usedCount: number;
  createdAt: string;
  updatedAt: string;
};

export type Account = {
  id: string;
  platformRoleId?: string;
  name: string;
  email: string;
  avatarUrl: string;
  role: RoleId | string;
  organization?: string;
  organizationId?: string;
  passwordHash: string;
  passwordSalt: string;
  status: AccountStatus;
  createdAt: string;
  updatedAt: string;
};

export type PublicAccount = Omit<Account, "passwordHash" | "passwordSalt">;

export type AccountOrganizationOption = Pick<
  Organization,
  "id" | "name" | "slug" | "status" | "registrationEnabled" | "oidcEnabled"
> & {
  roleLabel: string;
  isActive: boolean;
};

export type Session = {
  id: string;
  accountId: string;
  activeOrganizationId?: string;
  createdAt: string;
  expiresAt: string;
};

export type AuditResult = "success" | "denied" | "failure";

export type AuditLog = {
  id: string;
  organizationId?: string;
  timestamp: string;
  actorAccountId?: string;
  actorEmail?: string;
  action: string;
  targetType: string;
  targetId?: string;
  result: AuditResult;
  reason: string;
};

export type BedStatus = "available" | "occupied" | "maintenance" | "disabled";

export type FacilityBed = {
  id: string;
  organizationId: string;
  roomId: string;
  roomName: string;
  code: string;
  status: BedStatus;
  notes: string;
  currentElderName?: string;
  createdAt: string;
  updatedAt: string;
};

export type Room = {
  id: string;
  organizationId: string;
  name: string;
  code: string;
  capacity: number;
  status: OrganizationStatus;
  bedCount: number;
  occupiedBedCount: number;
  createdAt: string;
  updatedAt: string;
};

export type AdmissionStatus = "active" | "transferred" | "discharged";

export type Admission = {
  id: string;
  organizationId: string;
  elderId: string;
  elderName: string;
  bedId: string;
  bedCode: string;
  roomName: string;
  status: AdmissionStatus;
  admittedAt: string;
  dischargedAt?: string;
  notes: string;
};

export type IncidentSeverity = "info" | "warning" | "critical";
export type IncidentStatus = "open" | "acknowledged" | "resolved" | "closed";

export type SystemIncident = {
  id: string;
  organizationId?: string;
  severity: IncidentSeverity;
  status: IncidentStatus;
  title: string;
  description: string;
  source: string;
  createdAt: string;
  updatedAt: string;
};

export type AppData = {
  systemSettings: SystemSettings;
  organizations: Organization[];
  accounts: Account[];
  memberships: Membership[];
  joinRequests: JoinRequest[];
  organizationInvitations: OrganizationInvitation[];
  sessions: Session[];
  elders: Elder[];
  rooms: Room[];
  beds: FacilityBed[];
  admissions: Admission[];
  auditLogs: AuditLog[];
  incidents: SystemIncident[];
};

export type AuthContext = {
  account: PublicAccount;
  organization?: Organization;
  organizations: AccountOrganizationOption[];
  membership?: Membership;
  permissions: Permission[];
  session: Session;
};
