/**
 * Type definitions for LEAN Morning Meeting module
 */

// =============================================================================
// ENUMS
// =============================================================================

export type SQCDMCategory = 'SAFETY' | 'QUALITY' | 'COST' | 'DELIVERY' | 'MORALE';
export type MeetingStatus = 'SCHEDULED' | 'IN_PROGRESS' | 'COMPLETED' | 'CANCELLED';
export type KPIStatus = 'GREEN' | 'YELLOW' | 'RED' | 'NEUTRAL';
export type Trend = 'UP' | 'DOWN' | 'STABLE';
export type ActionStatus = 'OPEN' | 'IN_PROGRESS' | 'COMPLETED' | 'CANCELLED';
export type Priority = 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL';

// =============================================================================
// INTERFACES
// =============================================================================

export interface LeanUser {
  id: string;
  firstName: string;
  lastName: string;
  email: string;
  avatar?: string | null;
}

export interface LeanDepartment {
  id: string;
  name: string;
  code?: string | null;
}

export interface MorningMeetingTopic {
  id: string;
  category: SQCDMCategory;
  title: string;
  value?: string | null;
  target?: string | null;
  unit?: string | null;
  status: KPIStatus;
  trend?: Trend | null;
  sortOrder: number;
  note?: string | null;
  createdAt: string;
  updatedAt: string;
}

export interface MorningMeetingAction {
  id: string;
  title: string;
  description?: string | null;
  dueDate?: string | null;
  status: ActionStatus;
  priority: Priority;
  completedAt?: string | null;
  createdAt: string;
  updatedAt: string;
  assignee?: LeanUser | null;
}

export interface MorningMeeting {
  id: string;
  date: string;
  startTime?: string | null;
  endTime?: string | null;
  duration?: number | null;
  participants?: string[] | null;
  notes?: string | null;
  status: MeetingStatus;
  createdAt: string;
  updatedAt: string;
  department: LeanDepartment;
  conductor?: LeanUser | null;
  topics: MorningMeetingTopic[];
  actions: MorningMeetingAction[];
}

// =============================================================================
// API TYPES
// =============================================================================

export interface CreateMeetingDto {
  departmentId: string;
  date: string;
  conductorId?: string;
  participants?: string[];
  notes?: string;
  status?: MeetingStatus;
}

export interface UpdateMeetingDto {
  conductorId?: string;
  participants?: string[];
  notes?: string;
  status?: MeetingStatus;
  startTime?: string;
  endTime?: string;
  duration?: number;
}

export interface CreateTopicDto {
  category: SQCDMCategory;
  title: string;
  value?: string;
  target?: string;
  unit?: string;
  status?: KPIStatus;
  trend?: Trend;
  sortOrder?: number;
  note?: string;
}

export interface UpdateTopicDto {
  category?: SQCDMCategory;
  title?: string;
  value?: string;
  target?: string;
  unit?: string;
  status?: KPIStatus;
  trend?: Trend;
  sortOrder?: number;
  note?: string;
}

export interface CreateActionDto {
  title: string;
  description?: string;
  assigneeId?: string;
  dueDate?: string;
  status?: ActionStatus;
  priority?: Priority;
}

export interface UpdateActionDto {
  title?: string;
  description?: string;
  assigneeId?: string;
  dueDate?: string;
  status?: ActionStatus;
  priority?: Priority;
  completedAt?: string;
}

export interface MeetingsQueryParams {
  page?: number;
  limit?: number;
  departmentId?: string;
  status?: MeetingStatus;
  dateFrom?: string;
  dateTo?: string;
  conductorId?: string;
  sortBy?: 'date' | 'createdAt' | 'updatedAt';
  sortOrder?: 'asc' | 'desc';
}

export interface LeanPaginatedResponse<T> {
  data: T[];
  meta: {
    total: number;
    page: number;
    limit: number;
    totalPages: number;
  };
}

export interface KpiTrendData {
  departmentId: string;
  period: {
    from: string;
    to: string;
  };
  trends: Record<
    string,
    Array<{
      date: string;
      value: string | null;
      status: string;
      trend: string | null;
    }>
  >;
}

// =============================================================================
// HELPER TYPES
// =============================================================================

/**
 * SQCDM category metadata for UI display
 */
export const SQCDM_CATEGORIES: Record<
  SQCDMCategory,
  {
    labelKey: string;
    icon: string;
    color: string;
    bgColor: string;
  }
> = {
  SAFETY: {
    labelKey: 'safety',
    icon: 'ShieldCheck',
    color: 'text-orange-600',
    bgColor: 'bg-orange-50',
  },
  QUALITY: {
    labelKey: 'quality',
    icon: 'CheckCircle',
    color: 'text-blue-600',
    bgColor: 'bg-blue-50',
  },
  COST: {
    labelKey: 'cost',
    icon: 'DollarSign',
    color: 'text-green-600',
    bgColor: 'bg-green-50',
  },
  DELIVERY: {
    labelKey: 'delivery',
    icon: 'Truck',
    color: 'text-purple-600',
    bgColor: 'bg-purple-50',
  },
  MORALE: {
    labelKey: 'morale',
    icon: 'Users',
    color: 'text-pink-600',
    bgColor: 'bg-pink-50',
  },
};

/**
 * KPI status colors for traffic light display
 */
export const KPI_STATUS_COLORS: Record<
  KPIStatus,
  { bg: string; text: string; border: string }
> = {
  GREEN: {
    bg: 'bg-green-100',
    text: 'text-green-700',
    border: 'border-green-500',
  },
  YELLOW: {
    bg: 'bg-yellow-100',
    text: 'text-yellow-700',
    border: 'border-yellow-500',
  },
  RED: {
    bg: 'bg-red-100',
    text: 'text-red-700',
    border: 'border-red-500',
  },
  NEUTRAL: {
    bg: 'bg-gray-100',
    text: 'text-gray-700',
    border: 'border-gray-400',
  },
};

/**
 * Priority colors for action items
 */
export const PRIORITY_COLORS: Record<Priority, { bg: string; text: string }> = {
  LOW: { bg: 'bg-gray-100', text: 'text-gray-600' },
  MEDIUM: { bg: 'bg-blue-100', text: 'text-blue-600' },
  HIGH: { bg: 'bg-orange-100', text: 'text-orange-600' },
  CRITICAL: { bg: 'bg-red-100', text: 'text-red-600' },
};

/**
 * Action status colors
 */
export const ACTION_STATUS_COLORS: Record<
  ActionStatus,
  { bg: string; text: string }
> = {
  OPEN: { bg: 'bg-gray-100', text: 'text-gray-600' },
  IN_PROGRESS: { bg: 'bg-blue-100', text: 'text-blue-600' },
  COMPLETED: { bg: 'bg-green-100', text: 'text-green-600' },
  CANCELLED: { bg: 'bg-red-100', text: 'text-red-600' },
};
