export type Difficulty = "easy" | "medium" | "hard";

export interface CategoryBrief {
  id: string;
  name: string;
  slug: string;
}

export interface PatternBrief {
  id: string;
  name: string;
  slug: string;
}

export interface Category extends CategoryBrief {
  description: string | null;
  question_count: number;
}

export interface Pattern extends PatternBrief {
  description: string | null;
  when_to_use: string | null;
  question_count: number;
}

// Pattern Tutorial Types

export interface CommonMistake {
  title: string;
  description: string;
  fix: string | null;
}

export interface PatternVariation {
  name: string;
  description: string;
  example: string | null;
}

export interface RelatedPattern {
  slug: string;
  name: string;
  description: string | null;
}

export interface LearningQuestion {
  id: string;
  title: string;
  slug: string;
  difficulty: Difficulty;
  leetcode_id: number | null;
}

export interface LearningProgression {
  warmup: LearningQuestion[];
  core: LearningQuestion[];
  challenge: LearningQuestion[];
}

export interface VisualizationExampleStep {
  id: string;
  description: string;
  structures: Record<string, unknown>;
  variables?: Record<string, unknown>;
  codeHighlight?: { startLine: number; endLine: number };
}

export interface VisualizationExample {
  id: string;
  title: string;
  input?: Record<string, unknown>;
  code: string;
  steps: VisualizationExampleStep[];
}

export interface PatternTutorial extends Pattern {
  metaphor: string | null;
  core_concept: string | null;
  visualization: string | null;
  code_template: string | null;
  recognition_signals: string[] | null;
  common_mistakes: CommonMistake[] | null;
  variations: PatternVariation[] | null;
  related_patterns: RelatedPattern[] | null;
  prerequisite_patterns: RelatedPattern[] | null;
  difficulty_level: number | null;
  learning_progression: LearningProgression | null;
  visualization_examples: VisualizationExample[] | null;
}

export interface QuestionListItem {
  id: string;
  title: string;
  slug: string;
  difficulty: Difficulty;
  leetcode_id: number | null;
  leetcode_url: string | null;
  categories: CategoryBrief[];
  patterns: PatternBrief[];
  created_at: string;
}

export interface CommonPitfall {
  title: string;
  description: string;
  wrong_approach?: string;
  correct_approach?: string;
}

export interface Explanation {
  approach: string;
  intuition: string;
  common_pitfalls: CommonPitfall[] | null;
  key_takeaways: string[] | null;
  time_complexity: string;
  space_complexity: string;
  complexity_explanation: string | null;
}

export interface Solution {
  id: string;
  approach_name: string;
  code: string;
  language: string;
  is_optimal: boolean;
  explanation: string | null;
}

export interface VisibleTestCase {
  id: number;
  input: Record<string, unknown>;
  expected: unknown;
}

export interface HiddenTestInput {
  id: number;
  input: Record<string, unknown>;
}

export interface QuestionDetail extends QuestionListItem {
  description: string;
  constraints: string | null;
  examples: Array<{
    input: string;
    output: string;
    explanation?: string;
  }> | null;
  function_signature: string | null;
  visible_test_cases: VisibleTestCase[] | null;
  hidden_test_inputs: HiddenTestInput[] | null;
  explanation: Explanation | null;
  solutions: Solution[];
  updated_at: string;
}

export interface QuestionListResponse {
  items: QuestionListItem[];
  total: number;
  page: number;
  limit: number;
  pages: number;
}

export interface CategoryListResponse {
  items: Category[];
}

export interface PatternListResponse {
  items: Pattern[];
}

export interface DifficultyCount {
  easy: number;
  medium: number;
  hard: number;
}

export interface CategoryCount {
  name: string;
  slug: string;
  count: number;
}

export interface PatternCount {
  name: string;
  slug: string;
  count: number;
}

export interface Stats {
  total_questions: number;
  by_difficulty: DifficultyCount;
  by_category: CategoryCount[];
  by_pattern: PatternCount[];
}

export interface HiddenTestOutput {
  test_id: number;
  output: unknown;
}

export interface SubmissionRequest {
  code: string;
  hidden_outputs: HiddenTestOutput[];
}

export interface TestResult {
  test_id: number;
  passed: boolean;
  input?: Record<string, unknown>;
  expected?: unknown;
  actual: unknown;
  error?: string;
}

export interface SubmissionResponse {
  passed: boolean;
  visible_results: TestResult[];
  hidden_results: TestResult[];
  total_passed: number;
  total_tests: number;
}
