/**
 * 권한 관리 타입 정의
 */

// ========== API 응답 공통 타입 ==========

export interface ApiResponse<T> {
  success: boolean;
  data?: T;
  error?: string;
}

export interface PaginatedResponse<T> {
  data: T[];
  meta: {
    current_page: number;
    last_page: number;
    per_page: number;
    total: number;
  };
}

// ========== Role (역할) 타입 ==========

export interface Role {
  id: number;
  name: string;
  description?: string;
  is_hidden: boolean;
  guard_name: string;
  created_at: string;
  updated_at: string;
  permissions_count?: number;
  users_count?: number;
}

export interface RoleStats {
  total_roles: number;
  visible_roles: number;
  hidden_roles: number;
  roles_with_users: number;
}

// ========== Permission (권한) 타입 ==========

export type PermissionType = 'view' | 'create' | 'update' | 'delete' | 'approve' | 'export' | 'manage';

// 메뉴 트리 아이템 (권한 매트릭스용)
export interface MenuTreeItem {
  id: number;
  name: string;
  code: string;
  parent_id: number | null;
  depth: number;
  sort_order: number;
  children?: MenuTreeItem[];
}

// 권한 매트릭스 응답 (API 응답 구조에 맞춤)
export interface PermissionMatrix {
  role: {
    id: number;
    name: string;
    description?: string;
  };
  permission_types: PermissionType[];
  permissions: {
    [menuId: number]: {
      [key in PermissionType]?: boolean;
    };
  };
}

// 메뉴별 권한 상태
export interface MenuPermissionItem {
  menu_id: number;
  menu_name: string;
  menu_code: string;
  depth: number;
  permissions: {
    [key in PermissionType]?: boolean;
  };
}

// ========== Legacy 타입 (기존 호환성) ==========

// 메뉴별 권한 설정 (기존 호환)
export interface MenuPermission {
  menuId: string;
  menuName: string;
  parentMenuId?: string;
  permissions: {
    [key in PermissionType]?: boolean;
  };
}

// 권한 그룹 (기존 호환, Role로 대체 권장)
export interface Permission {
  id: number;
  name: string;
  status: 'active' | 'hidden'; // 공개/숨김
  menuPermissions: MenuPermission[];
  createdAt: string;
  updatedAt?: string;
}

// ========== Dialog Props ==========

export interface RoleDialogProps {
  isOpen: boolean;
  onOpenChange: (open: boolean) => void;
  mode: 'add' | 'edit';
  role?: Role;
  onSubmit: (data: { name: string; description?: string; is_hidden: boolean }) => void;
}

// 기존 호환성
export interface PermissionDialogProps {
  isOpen: boolean;
  onOpenChange: (open: boolean) => void;
  mode: 'add' | 'edit';
  permission?: Permission;
  onSubmit: (data: { name: string; status: 'active' | 'hidden' }) => void;
}

// ========== 상수 ==========

// 권한 라벨 매핑
export const PERMISSION_LABELS: Record<PermissionType, string> = {
  view: '조회',
  create: '생성',
  update: '수정',
  delete: '삭제',
  approve: '승인',
  export: '내보내기',
  manage: '관리',
};

// 역할 상태 라벨
export const ROLE_STATUS_LABELS = {
  visible: '공개',
  hidden: '숨김',
} as const;
