import type { Permission } from '@/lib/auth/permissions';

export interface PermissionTemplate {
  id: number;
  organization_id: string;
  code: string;
  name: string;
  description: string | null;
  is_system: boolean;
  is_default: boolean;
  is_active: boolean;
  deleted_at: string | null;
  assigned_user_count: number;
  permissions: Permission[];
  created_at: string;
  updated_at: string;
}

export interface PermissionTemplateFilters {
  page?: number;
  limit?: number;
  search?: string;
  status?: string;
  sort?: string;
}

export interface PermissionTemplatesResponse {
  success: boolean;
  time: string;
  message: string;
  total_permission_templates: number;
  offset: number;
  limit: number;
  permission_templates: PermissionTemplate[];
}

export interface PermissionTemplateMutationPayload {
  code: string;
  name: string;
  description?: string;
  is_default: boolean;
  is_active: boolean;
  permissions: Permission[];
  reason?: string;
}

export interface PermissionCatalogGroup {
  key: string;
  label: string;
  permissions: PermissionDefinition[];
}

export interface PermissionDefinition {
  module: string;
  action: string;
  code: Permission;
  name: string;
  description: string;
  descriptionEn?: string;
  value: Permission;
  label: string;
}

export interface PermissionCatalogResponse {
  success: boolean;
  groups: PermissionCatalogGroup[];
}

export interface UserPermissionAssignmentListItem {
  user_id: string;
  employee_code: string | null;
  full_name: string;
  email: string;
  department_name: string | null;
  position_name: string | null;
  template_id: number | null;
  template_name: string | null;
  override_count: number;
  updated_at: string;
}

export interface UserPermissionAssignmentFilters {
  page?: number;
  limit?: number;
  search?: string;
  sort?: string;
}

export interface UserPermissionAssignmentsResponse {
  success: boolean;
  time: string;
  message: string;
  total_user_permissions: number;
  offset: number;
  limit: number;
  user_permissions: UserPermissionAssignmentListItem[];
}

export interface PermissionTemplateOption {
  id: number;
  name: string;
  code: string;
  is_default: boolean;
  permissions: Permission[];
}

export interface UserPermissionOverride {
  id: number;
  permission: Permission;
  effect: 'allow' | 'deny';
  reason: string | null;
  expires_at: string | null;
  is_active: boolean;
  created_at: string;
}

export interface UserPermissionAssignmentDetail {
  user: {
    id: string;
    full_name: string;
    email: string;
    employee_code: string | null;
    department_name: string | null;
    position_name: string | null;
  };
  assignment: {
    template_id: number | null;
    template_name: string | null;
    permission_template_ids: number[];
  };
  templates: PermissionTemplateOption[];
  overrides: UserPermissionOverride[];
  effective_permissions: Permission[];
  fallback_permissions: Permission[];
}

export interface UserPermissionAssignmentMutationPayload {
  template_id: number;
  allow_permissions: Permission[];
  deny_permissions: Permission[];
  reason?: string;
}

export interface UserPermissionOverrideMutationPayload {
  permission: Permission;
  effect: 'allow' | 'deny';
  reason?: string;
  expires_at?: string | null;
}

export interface PermissionAuditLog {
  id: number;
  actor_user_name: string | null;
  target_user_name: string | null;
  template_name: string | null;
  action: string;
  reason: string | null;
  before: Record<string, unknown> | null;
  after: Record<string, unknown> | null;
  created_at: string;
}

export interface PermissionAuditLogFilters {
  page?: number;
  limit?: number;
  search?: string;
  user?: string;
  action?: string;
  dateFrom?: string;
  dateTo?: string;
  sort?: string;
}

export interface PermissionAuditLogsResponse {
  success: boolean;
  time: string;
  message: string;
  total_permission_audit_logs: number;
  offset: number;
  limit: number;
  permission_audit_logs: PermissionAuditLog[];
}
