export type Role = 'admin' | 'redteam' | 'soc';

export type EngagementStatus = 'planned' | 'active' | 'closed';

export interface User {
  id: number;
  username: string;
  role: Role;
  created_at: string;
}

export interface LoginResponse {
  access_token: string;
  user: Pick<User, 'id' | 'username' | 'role'>;
}

export interface EngagementCreatedBy {
  id: number;
  username: string;
}

export interface Engagement {
  id: number;
  name: string;
  description: string | null;
  start_date: string;
  end_date: string | null;
  status: EngagementStatus;
  created_at: string;
  created_by: EngagementCreatedBy;
}

export interface EngagementInput {
  name: string;
  description?: string;
  start_date: string;
  end_date?: string | null;
  status?: EngagementStatus;
}

export interface UserCreateInput {
  username: string;
  password: string;
  role: Role;
}

export interface UserPatchInput {
  role?: Role;
  password?: string;
}

export interface ApiError {
  error: string;
}

export type SimulationStatus = 'pending' | 'in_progress' | 'review_required' | 'done';

export interface MitreTechnique {
  id: string;
  name: string;
  tactics: string[];
}

export interface MitreMatrixSubtechnique {
  id: string;
  name: string;
}

export interface MitreMatrixTechnique {
  id: string;
  name: string;
  subtechniques: MitreMatrixSubtechnique[];
}

export interface MitreTactic {
  tactic_id: string;
  tactic_name: string;
  techniques: MitreMatrixTechnique[];
}

export interface MitreTacticRef {
  id: string;
  name: string;
}

export interface Simulation {
  id: number;
  engagement_id: number;
  name: string;
  techniques: MitreTechnique[];
  tactics: MitreTacticRef[];
  description: string | null;
  commands: string | null;
  prerequisites: string | null;
  executed_at: string | null;
  execution_result: string | null;
  log_source: string | null;
  logs: string | null;
  soc_comment: string | null;
  incident_number: string | null;
  status: SimulationStatus;
  created_at: string;
  updated_at: string | null;
  created_by: { id: number; username: string };
}

export interface SimulationTemplate {
  id: number;
  name: string;
  description: string | null;
  commands: string | null;
  prerequisites: string | null;
  techniques: MitreTechnique[];
  tactics: MitreTacticRef[];
  created_at: string;
  updated_at: string | null;
  created_by: { id: number; username: string };
}

export interface SimulationTemplateCreateInput {
  name: string;
  description?: string | null;
  commands?: string | null;
  prerequisites?: string | null;
  technique_ids?: string[];
  tactic_ids?: string[];
}

export interface SimulationTemplatePatchInput {
  name?: string;
  description?: string | null;
  commands?: string | null;
  prerequisites?: string | null;
  technique_ids?: string[];
  tactic_ids?: string[];
}

export interface SimulationCreateInput {
  name: string;
  template_id?: number;
}

export interface SimulationPatchInput {
  name?: string;
  technique_ids?: string[];
  tactic_ids?: string[];
  description?: string | null;
  commands?: string | null;
  prerequisites?: string | null;
  executed_at?: string | null;
  execution_result?: string | null;
  log_source?: string | null;
  logs?: string | null;
  soc_comment?: string | null;
  incident_number?: string | null;
}

// C2 types

export interface C2Config {
  has_token: boolean;
  url: string;
  verify_tls: boolean;
}

export interface C2ConfigInput {
  url: string;
  api_token?: string;
  verify_tls: boolean;
}

export interface C2TestResult {
  ok: boolean;
  error: string | null;
}

export interface C2Callback {
  display_id: number;
  active: boolean;
  host: string;
  user: string;
  domain: string;
  last_checkin: string;
}

export interface C2CallbacksResponse {
  callbacks: C2Callback[];
}

// Thin shape returned by the execute endpoint
export interface C2ExecuteTask {
  id: number;
  mythic_task_display_id: number;
  command: string;
  status: string;
  completed: boolean;
}

export interface C2ExecuteInput {
  callback_display_id: number;
  commands: string[];
}

export interface C2ExecuteResponse {
  tasks: C2ExecuteTask[];
}

// Full shape returned by the tasks list endpoint (M3)
export interface C2TaskListItem {
  id: number;
  mythic_task_display_id: number;
  callback_display_id: number;
  command: string;
  params: string | null;
  status: string;
  completed: boolean;
  output: string | null;
  mapping_applied: boolean;
  source: 'mimic' | 'import';
  created_at: string;
  completed_at: string | null;
}

export interface C2TasksResponse {
  tasks: C2TaskListItem[];
}

// Callback history (M4)
export interface C2HistoryTask {
  display_id: number;
  command: string;
  status: string;
  completed: boolean;
  completed_at: string | null;
  created_at: string;
}

export interface C2CallbackHistoryResponse {
  tasks: C2HistoryTask[];
  total: number;
  page: number;
  page_size: number;
}

// Import (M4)
export interface C2ImportInput {
  callback_display_id: number;
  task_display_ids: number[];
}

export interface C2ImportResponse {
  imported: number;
  skipped: number;
}
