/* ── Shared TypeScript types matching the backend Pydantic schemas ── */

// ── Users ──────────────────────────────────────────────────────────

export interface User {
  id: string;
  username: string;
  role: string;
  must_change_password?: boolean;
}

// ── Techniques ─────────────────────────────────────────────────────

export interface TechniqueExternalReference {
  source_name: string;
  url?: string;
  external_id?: string;
  description?: string;
}

export interface Technique {
  id: string;
  mitre_id: string;
  name: string;
  description: string | null;
  tactic: string | null;
  platforms: string[];
  mitre_version: string | null;
  mitre_last_modified: string | null;
  is_subtechnique: boolean;
  parent_mitre_id: string | null;
  status_global: TechniqueStatus;
  review_required: boolean;
  last_review_date: string | null;
  external_references?: TechniqueExternalReference[];
}

export type TechniqueStatus =
  | "not_evaluated"
  | "in_progress"
  | "validated"
  | "partial"
  | "not_covered"
  | "review_required";

// ── Tests (v2 — Red/Blue dual validation) ──────────────────────────

export type TestState =
  | "draft"
  | "red_executing"
  | "red_review"        // Red Lead reviews the operator's work before it queues for Blue Team
  | "blue_evaluating"
  | "blue_review"        // Blue Lead reviews the operator's work before cross-validation
  | "in_review"
  | "validated"
  | "rejected"
  | "disputed";   // one lead approved, the other rejected — needs discussion

export type TestResult = "detected" | "not_detected" | "partially_detected";

export type ContainmentResult = "contained" | "partially_contained" | "not_contained";

export type AttackSuccessResult = "successful" | "not_successful" | "partially_successful";

export type DataClassification = "public_release" | "general_use" | "confidential" | "restricted";

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

export type TeamSide = "red" | "blue";

/** States in which Red Team can edit / upload evidence. */
export const RED_EDITABLE_STATES: TestState[] = ["draft", "red_executing"];

/** States in which Blue Team can edit / upload evidence. */
export const BLUE_EDITABLE_STATES: TestState[] = ["blue_evaluating"];

export interface Test {
  id: string;
  technique_id: string;
  name: string;
  description: string | null;
  platform: string | null;
  procedure_text: string | null;
  tool_used: string | null;
  execution_date: string | null;
  created_by: string | null;
  result: TestResult | null;
  state: TestState;
  created_at: string;
  updated_at: string | null;

  // Red Team fields
  red_summary: string | null;
  attack_success: AttackSuccessResult | null;
  execution_start_time: string | null;
  execution_end_time: string | null;
  red_validated_by: string | null;
  red_validated_at: string | null;
  red_validation_status: ValidationStatus | null;
  red_validation_notes: string | null;

  // Blue Team fields
  blue_summary: string | null;
  detection_result: TestResult | null;
  containment_result: ContainmentResult | null;
  detection_time: string | null;
  containment_time: string | null;
  blue_validated_by: string | null;
  blue_validated_at: string | null;
  blue_validation_status: ValidationStatus | null;
  blue_validation_notes: string | null;

  // Phase timing fields (for automatic Tempo worklogs)
  red_started_at: string | null;
  blue_started_at: string | null;
  blue_work_started_at: string | null;
  paused_at: string | null;
  red_paused_seconds: number;
  blue_paused_seconds: number;

  // Remediation fields
  remediation_steps: string | null;
  remediation_status: string | null;
  remediation_assignee: string | null;

  // Assignment fields
  red_tech_assignee: string | null;
  blue_tech_assignee: string | null;

  // Red Team review fields
  red_reviewer_assignee: string | null;
  red_review_by: string | null;
  red_review_at: string | null;
  red_review_notes: string | null;

  // Blue Team review fields
  blue_reviewer_assignee: string | null;
  blue_review_by: string | null;
  blue_review_at: string | null;
  blue_review_notes: string | null;
  system_gaps: string | null;

  // On-hold fields
  is_on_hold: boolean;
  hold_reason: string | null;
  held_at: string | null;

  // Re-test fields
  retest_of: string | null;
  retest_count: number;

  // Data classification
  data_classification: DataClassification;

  // Technique info (populated in list endpoints)
  technique_mitre_id: string | null;
  technique_name: string | null;

  // Separated evidences
  red_evidences: Evidence[];
  blue_evidences: Evidence[];
}

// ── Evidence (v2 — with team) ──────────────────────────────────────

export interface Evidence {
  id: string;
  test_id: string;
  file_name: string;
  file_path: string;
  sha256_hash: string;
  uploaded_by: string | null;
  uploaded_at: string;
  team: TeamSide;
  notes: string | null;
  download_url?: string;
}

// ── Test Templates ─────────────────────────────────────────────────

export interface TestTemplate {
  id: string;
  mitre_technique_id: string;
  name: string;
  description: string | null;
  source: string;
  source_url: string | null;
  attack_procedure: string | null;
  expected_detection: string | null;
  platform: string | null;
  tool_suggested: string | null;
  severity: string | null;
  atomic_test_id: string | null;
  suggested_remediation: string | null;
  is_active: boolean;
  created_at: string;
}

export interface TestTemplateSummary {
  id: string;
  mitre_technique_id: string;
  name: string;
  source: string;
  platform: string | null;
  severity: string | null;
}

// ── Timeline ───────────────────────────────────────────────────────

export interface TestTimelineEntry {
  id: string;
  action: string;
  user_id: string | null;
  timestamp: string;
  details: Record<string, unknown>;
}

// ── Intel ──────────────────────────────────────────────────────────

export interface IntelItem {
  id: string;
  technique_id: string | null;
  url: string;
  title: string | null;
  source: string | null;
  detected_at: string;
  reviewed: boolean;
}

// ── Metrics ────────────────────────────────────────────────────────

export interface CoverageSummary {
  total_techniques: number;
  validated: number;
  partial: number;
  not_covered: number;
  in_progress: number;
  not_evaluated: number;
  coverage_percentage: number;
}

export interface TacticCoverage {
  tactic: string;
  total: number;
  validated: number;
  partial: number;
  not_covered: number;
  not_evaluated: number;
  in_progress: number;
}

// ── D3FEND ────────────────────────────────────────────────────────

export interface DefensiveTechnique {
  id: string;
  d3fend_id: string;
  name: string;
  description: string | null;
  tactic: string | null;
  d3fend_url: string | null;
}

// ── Jira ──────────────────────────────────────────────────────────

export type JiraLinkEntityType = "test" | "technique" | "campaign" | "evidence";

export interface JiraLink {
  id: string;
  entity_type: JiraLinkEntityType;
  entity_id: string;
  jira_issue_key: string;
  jira_status: string | null;
  jira_priority: string | null;
  jira_assignee: string | null;
  last_synced_at: string | null;
  created_at: string;
}

// ── Worklogs ──────────────────────────────────────────────────────

export interface Worklog {
  id: string;
  entity_type: string;
  entity_id: string;
  user_id: string;
  activity_type: string;
  started_at: string;
  ended_at: string | null;
  duration_seconds: number;
  description: string | null;
  tempo_synced: string | null;
  integrity_hash: string | null;
  created_at: string;
}
