import type { AlgorithmDefinition } from '@/lib/visualizations/types';

export const slidingWindowAlgorithm: AlgorithmDefinition = {
  id: 'max-sum-subarray',
  title: 'Maximum Sum Subarray of Size K',
  slug: 'max-sum-subarray',
  pattern: {
    name: 'Sliding Window',
    description:
      'Maintain a window of elements that slides through the array, updating state incrementally.',
  },
  problemStatement:
    'Given an array of integers and a window size k, find the maximum sum of any contiguous subarray of size k.',
  intuition:
    'Instead of recalculating the sum for each window from scratch (O(n*k)), we can slide the window by subtracting the element leaving and adding the element entering (O(n)).',
  code: {
    language: 'python',
    code: `def max_sum_subarray(nums: list[int], k: int) -> int:
    window_sum = sum(nums[:k])
    max_sum = window_sum

    for i in range(k, len(nums)):
        window_sum += nums[i] - nums[i - k]
        max_sum = max(max_sum, window_sum)

    return max_sum`,
  },
  initialExample: {
    input: { nums: [2, 1, 5, 1, 3, 2], k: 3 },
    expected: 9,
  },
  steps: [
    // Phase 1: Problem (2 steps)
    {
      id: 'problem-1',
      phase: 'problem',
      explanation:
        'We have an array [2, 1, 5, 1, 3, 2] and need to find the maximum sum of any 3 consecutive elements.',
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'normal' },
              { value: 1, index: 1, state: 'normal' },
              { value: 5, index: 2, state: 'normal' },
              { value: 1, index: 3, state: 'normal' },
              { value: 3, index: 4, state: 'normal' },
              { value: 2, index: 5, state: 'normal' },
            ],
          },
        ],
        pointers: [],
        variables: [{ id: 'k', name: 'k', value: 3 }],
        calculations: [],
      },
    },
    {
      id: 'problem-2',
      phase: 'problem',
      explanation:
        'A brute force approach would sum each window separately, giving O(n*k) time. Can we avoid redundant calculations?',
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'highlighted' },
              { value: 1, index: 1, state: 'highlighted' },
              { value: 5, index: 2, state: 'highlighted' },
              { value: 1, index: 3, state: 'normal' },
              { value: 3, index: 4, state: 'normal' },
              { value: 2, index: 5, state: 'normal' },
            ],
          },
        ],
        pointers: [],
        variables: [{ id: 'k', name: 'k', value: 3 }],
        calculations: [],
      },
    },

    // Phase 2: Intuition (3 steps)
    {
      id: 'intuition-1',
      phase: 'intuition',
      explanation:
        'Key insight: When sliding the window one position right, we only need to subtract the leaving element and add the entering element.',
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'highlighted' },
              { value: 1, index: 1, state: 'highlighted' },
              { value: 5, index: 2, state: 'highlighted' },
              { value: 1, index: 3, state: 'normal' },
              { value: 3, index: 4, state: 'normal' },
              { value: 2, index: 5, state: 'normal' },
            ],
          },
        ],
        pointers: [
          { id: 'left', name: 'left', index: 0, color: 'left' },
          { id: 'right', name: 'right', index: 2, color: 'right' },
        ],
        variables: [{ id: 'k', name: 'k', value: 3 }],
        calculations: [],
      },
    },
    {
      id: 'intuition-2',
      phase: 'intuition',
      explanation:
        'Window slides: element at left exits (subtract), element after right enters (add). This gives O(1) per slide!',
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'dimmed' },
              { value: 1, index: 1, state: 'highlighted' },
              { value: 5, index: 2, state: 'highlighted' },
              { value: 1, index: 3, state: 'highlighted' },
              { value: 3, index: 4, state: 'normal' },
              { value: 2, index: 5, state: 'normal' },
            ],
          },
        ],
        pointers: [
          { id: 'left', name: 'left', index: 1, color: 'left' },
          { id: 'right', name: 'right', index: 3, color: 'right' },
        ],
        variables: [{ id: 'k', name: 'k', value: 3 }],
        calculations: [],
      },
    },
    {
      id: 'intuition-3',
      phase: 'intuition',
      explanation:
        'Track the maximum sum seen so far as the window slides through the entire array.',
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'normal' },
              { value: 1, index: 1, state: 'normal' },
              { value: 5, index: 2, state: 'normal' },
              { value: 1, index: 3, state: 'normal' },
              { value: 3, index: 4, state: 'normal' },
              { value: 2, index: 5, state: 'normal' },
            ],
          },
        ],
        pointers: [],
        variables: [
          { id: 'k', name: 'k', value: 3 },
          { id: 'max_sum', name: 'max_sum', value: '?' },
        ],
        calculations: [],
      },
    },

    // Phase 3: Code walkthrough (4 steps)
    {
      id: 'code-1',
      phase: 'code',
      explanation:
        'First, calculate the sum of the initial window (first k elements).',
      codeLine: 2,
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'normal' },
              { value: 1, index: 1, state: 'normal' },
              { value: 5, index: 2, state: 'normal' },
              { value: 1, index: 3, state: 'normal' },
              { value: 3, index: 4, state: 'normal' },
              { value: 2, index: 5, state: 'normal' },
            ],
          },
        ],
        pointers: [],
        variables: [{ id: 'k', name: 'k', value: 3 }],
        calculations: [],
      },
    },
    {
      id: 'code-2',
      phase: 'code',
      explanation:
        'Initialize max_sum with the initial window sum. This is our best answer so far.',
      codeLine: 3,
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'normal' },
              { value: 1, index: 1, state: 'normal' },
              { value: 5, index: 2, state: 'normal' },
              { value: 1, index: 3, state: 'normal' },
              { value: 3, index: 4, state: 'normal' },
              { value: 2, index: 5, state: 'normal' },
            ],
          },
        ],
        pointers: [],
        variables: [{ id: 'k', name: 'k', value: 3 }],
        calculations: [],
      },
    },
    {
      id: 'code-3',
      phase: 'code',
      explanation:
        'Slide the window: add the new element nums[i], subtract the leaving element nums[i-k].',
      codeLine: 6,
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'normal' },
              { value: 1, index: 1, state: 'normal' },
              { value: 5, index: 2, state: 'normal' },
              { value: 1, index: 3, state: 'normal' },
              { value: 3, index: 4, state: 'normal' },
              { value: 2, index: 5, state: 'normal' },
            ],
          },
        ],
        pointers: [],
        variables: [{ id: 'k', name: 'k', value: 3 }],
        calculations: [],
      },
    },
    {
      id: 'code-4',
      phase: 'code',
      explanation:
        'Update max_sum if the current window sum is larger.',
      codeLine: 7,
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'normal' },
              { value: 1, index: 1, state: 'normal' },
              { value: 5, index: 2, state: 'normal' },
              { value: 1, index: 3, state: 'normal' },
              { value: 3, index: 4, state: 'normal' },
              { value: 2, index: 5, state: 'normal' },
            ],
          },
        ],
        pointers: [],
        variables: [{ id: 'k', name: 'k', value: 3 }],
        calculations: [],
      },
    },

    // Phase 4: Execution (~15 steps)
    {
      id: 'exec-1',
      phase: 'execution',
      explanation:
        'Calculate the initial window sum: nums[0] + nums[1] + nums[2] = 2 + 1 + 5 = 8.',
      codeLine: 2,
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'highlighted' },
              { value: 1, index: 1, state: 'highlighted' },
              { value: 5, index: 2, state: 'highlighted' },
              { value: 1, index: 3, state: 'normal' },
              { value: 3, index: 4, state: 'normal' },
              { value: 2, index: 5, state: 'normal' },
            ],
          },
        ],
        pointers: [
          { id: 'left', name: 'left', index: 0, color: 'left' },
          { id: 'right', name: 'right', index: 2, color: 'right' },
        ],
        variables: [
          { id: 'k', name: 'k', value: 3 },
          { id: 'window_sum', name: 'window_sum', value: 8, derivation: '2 + 1 + 5' },
        ],
        calculations: [
          { id: 'calc-1', expression: '2 + 1 + 5', result: '8', position: 'above' },
        ],
      },
    },
    {
      id: 'exec-2',
      phase: 'execution',
      explanation: 'Initialize max_sum = window_sum = 8.',
      codeLine: 3,
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'highlighted' },
              { value: 1, index: 1, state: 'highlighted' },
              { value: 5, index: 2, state: 'highlighted' },
              { value: 1, index: 3, state: 'normal' },
              { value: 3, index: 4, state: 'normal' },
              { value: 2, index: 5, state: 'normal' },
            ],
          },
        ],
        pointers: [
          { id: 'left', name: 'left', index: 0, color: 'left' },
          { id: 'right', name: 'right', index: 2, color: 'right' },
        ],
        variables: [
          { id: 'k', name: 'k', value: 3 },
          { id: 'window_sum', name: 'window_sum', value: 8 },
          { id: 'max_sum', name: 'max_sum', value: 8 },
        ],
        calculations: [],
      },
    },
    {
      id: 'exec-3',
      phase: 'execution',
      explanation: 'Start sliding: i = 3 (k). Loop condition: i < len(nums) = 6? Yes.',
      codeLine: 5,
      decision: {
        question: 'Is i < len(nums)?',
        answer: '3 < 6 = true',
        action: 'Enter the for loop with i = 3',
      },
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'highlighted' },
              { value: 1, index: 1, state: 'highlighted' },
              { value: 5, index: 2, state: 'highlighted' },
              { value: 1, index: 3, state: 'comparing' },
              { value: 3, index: 4, state: 'normal' },
              { value: 2, index: 5, state: 'normal' },
            ],
          },
        ],
        pointers: [
          { id: 'left', name: 'left', index: 0, color: 'left' },
          { id: 'right', name: 'right', index: 2, color: 'right' },
          { id: 'i', name: 'i', index: 3, color: 'mid' },
        ],
        variables: [
          { id: 'k', name: 'k', value: 3 },
          { id: 'window_sum', name: 'window_sum', value: 8 },
          { id: 'max_sum', name: 'max_sum', value: 8 },
          { id: 'i', name: 'i', value: 3 },
        ],
        calculations: [],
      },
    },
    {
      id: 'exec-4',
      phase: 'execution',
      explanation: 'Slide window: add nums[3]=1, subtract nums[0]=2. New sum: 8 + 1 - 2 = 7.',
      codeLine: 6,
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'dimmed' },
              { value: 1, index: 1, state: 'highlighted' },
              { value: 5, index: 2, state: 'highlighted' },
              { value: 1, index: 3, state: 'highlighted' },
              { value: 3, index: 4, state: 'normal' },
              { value: 2, index: 5, state: 'normal' },
            ],
          },
        ],
        pointers: [
          { id: 'left', name: 'left', index: 1, color: 'left' },
          { id: 'right', name: 'right', index: 3, color: 'right' },
        ],
        variables: [
          { id: 'k', name: 'k', value: 3 },
          { id: 'window_sum', name: 'window_sum', value: 7, previousValue: 8, derivation: '8 + 1 - 2' },
          { id: 'max_sum', name: 'max_sum', value: 8 },
          { id: 'i', name: 'i', value: 3 },
        ],
        calculations: [
          { id: 'calc-2', expression: '8 + 1 - 2', result: '7', position: 'above' },
        ],
      },
    },
    {
      id: 'exec-5',
      phase: 'execution',
      explanation: 'Compare: window_sum (7) vs max_sum (8). 7 < 8, so max_sum stays 8.',
      codeLine: 7,
      decision: {
        question: 'Is window_sum > max_sum?',
        answer: '7 > 8 = false',
        action: 'Keep max_sum = 8',
      },
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'dimmed' },
              { value: 1, index: 1, state: 'highlighted' },
              { value: 5, index: 2, state: 'highlighted' },
              { value: 1, index: 3, state: 'highlighted' },
              { value: 3, index: 4, state: 'normal' },
              { value: 2, index: 5, state: 'normal' },
            ],
          },
        ],
        pointers: [
          { id: 'left', name: 'left', index: 1, color: 'left' },
          { id: 'right', name: 'right', index: 3, color: 'right' },
        ],
        variables: [
          { id: 'k', name: 'k', value: 3 },
          { id: 'window_sum', name: 'window_sum', value: 7 },
          { id: 'max_sum', name: 'max_sum', value: 8 },
          { id: 'i', name: 'i', value: 3 },
        ],
        calculations: [
          { id: 'calc-2', expression: '7 > 8', result: 'false', position: 'above' },
        ],
      },
    },
    {
      id: 'exec-6',
      phase: 'execution',
      explanation: 'Next iteration: i = 4. Loop condition: 4 < 6? Yes.',
      codeLine: 5,
      decision: {
        question: 'Is i < len(nums)?',
        answer: '4 < 6 = true',
        action: 'Continue for loop with i = 4',
      },
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'dimmed' },
              { value: 1, index: 1, state: 'highlighted' },
              { value: 5, index: 2, state: 'highlighted' },
              { value: 1, index: 3, state: 'highlighted' },
              { value: 3, index: 4, state: 'comparing' },
              { value: 2, index: 5, state: 'normal' },
            ],
          },
        ],
        pointers: [
          { id: 'left', name: 'left', index: 1, color: 'left' },
          { id: 'right', name: 'right', index: 3, color: 'right' },
          { id: 'i', name: 'i', index: 4, color: 'mid' },
        ],
        variables: [
          { id: 'k', name: 'k', value: 3 },
          { id: 'window_sum', name: 'window_sum', value: 7 },
          { id: 'max_sum', name: 'max_sum', value: 8 },
          { id: 'i', name: 'i', value: 4, previousValue: 3 },
        ],
        calculations: [],
      },
    },
    {
      id: 'exec-7',
      phase: 'execution',
      explanation: 'Slide window: add nums[4]=3, subtract nums[1]=1. New sum: 7 + 3 - 1 = 9.',
      codeLine: 6,
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'dimmed' },
              { value: 1, index: 1, state: 'dimmed' },
              { value: 5, index: 2, state: 'highlighted' },
              { value: 1, index: 3, state: 'highlighted' },
              { value: 3, index: 4, state: 'highlighted' },
              { value: 2, index: 5, state: 'normal' },
            ],
          },
        ],
        pointers: [
          { id: 'left', name: 'left', index: 2, color: 'left' },
          { id: 'right', name: 'right', index: 4, color: 'right' },
        ],
        variables: [
          { id: 'k', name: 'k', value: 3 },
          { id: 'window_sum', name: 'window_sum', value: 9, previousValue: 7, derivation: '7 + 3 - 1' },
          { id: 'max_sum', name: 'max_sum', value: 8 },
          { id: 'i', name: 'i', value: 4 },
        ],
        calculations: [
          { id: 'calc-3', expression: '7 + 3 - 1', result: '9', position: 'above' },
        ],
      },
    },
    {
      id: 'exec-8',
      phase: 'execution',
      explanation: 'Compare: window_sum (9) > max_sum (8)! Update max_sum to 9.',
      codeLine: 7,
      decision: {
        question: 'Is window_sum > max_sum?',
        answer: '9 > 8 = true',
        action: 'Update max_sum = 9',
      },
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'dimmed' },
              { value: 1, index: 1, state: 'dimmed' },
              { value: 5, index: 2, state: 'success' },
              { value: 1, index: 3, state: 'success' },
              { value: 3, index: 4, state: 'success' },
              { value: 2, index: 5, state: 'normal' },
            ],
          },
        ],
        pointers: [
          { id: 'left', name: 'left', index: 2, color: 'left' },
          { id: 'right', name: 'right', index: 4, color: 'right' },
        ],
        variables: [
          { id: 'k', name: 'k', value: 3 },
          { id: 'window_sum', name: 'window_sum', value: 9 },
          { id: 'max_sum', name: 'max_sum', value: 9, previousValue: 8 },
          { id: 'i', name: 'i', value: 4 },
        ],
        calculations: [
          { id: 'calc-3', expression: '9 > 8', result: 'true', position: 'above' },
        ],
      },
    },
    {
      id: 'exec-9',
      phase: 'execution',
      explanation: 'Next iteration: i = 5. Loop condition: 5 < 6? Yes.',
      codeLine: 5,
      decision: {
        question: 'Is i < len(nums)?',
        answer: '5 < 6 = true',
        action: 'Continue for loop with i = 5',
      },
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'dimmed' },
              { value: 1, index: 1, state: 'dimmed' },
              { value: 5, index: 2, state: 'highlighted' },
              { value: 1, index: 3, state: 'highlighted' },
              { value: 3, index: 4, state: 'highlighted' },
              { value: 2, index: 5, state: 'comparing' },
            ],
          },
        ],
        pointers: [
          { id: 'left', name: 'left', index: 2, color: 'left' },
          { id: 'right', name: 'right', index: 4, color: 'right' },
          { id: 'i', name: 'i', index: 5, color: 'mid' },
        ],
        variables: [
          { id: 'k', name: 'k', value: 3 },
          { id: 'window_sum', name: 'window_sum', value: 9 },
          { id: 'max_sum', name: 'max_sum', value: 9 },
          { id: 'i', name: 'i', value: 5, previousValue: 4 },
        ],
        calculations: [],
      },
    },
    {
      id: 'exec-10',
      phase: 'execution',
      explanation: 'Slide window: add nums[5]=2, subtract nums[2]=5. New sum: 9 + 2 - 5 = 6.',
      codeLine: 6,
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'dimmed' },
              { value: 1, index: 1, state: 'dimmed' },
              { value: 5, index: 2, state: 'dimmed' },
              { value: 1, index: 3, state: 'highlighted' },
              { value: 3, index: 4, state: 'highlighted' },
              { value: 2, index: 5, state: 'highlighted' },
            ],
          },
        ],
        pointers: [
          { id: 'left', name: 'left', index: 3, color: 'left' },
          { id: 'right', name: 'right', index: 5, color: 'right' },
        ],
        variables: [
          { id: 'k', name: 'k', value: 3 },
          { id: 'window_sum', name: 'window_sum', value: 6, previousValue: 9, derivation: '9 + 2 - 5' },
          { id: 'max_sum', name: 'max_sum', value: 9 },
          { id: 'i', name: 'i', value: 5 },
        ],
        calculations: [
          { id: 'calc-4', expression: '9 + 2 - 5', result: '6', position: 'above' },
        ],
      },
    },
    {
      id: 'exec-11',
      phase: 'execution',
      explanation: 'Compare: window_sum (6) vs max_sum (9). 6 < 9, so max_sum stays 9.',
      codeLine: 7,
      decision: {
        question: 'Is window_sum > max_sum?',
        answer: '6 > 9 = false',
        action: 'Keep max_sum = 9',
      },
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'dimmed' },
              { value: 1, index: 1, state: 'dimmed' },
              { value: 5, index: 2, state: 'dimmed' },
              { value: 1, index: 3, state: 'highlighted' },
              { value: 3, index: 4, state: 'highlighted' },
              { value: 2, index: 5, state: 'highlighted' },
            ],
          },
        ],
        pointers: [
          { id: 'left', name: 'left', index: 3, color: 'left' },
          { id: 'right', name: 'right', index: 5, color: 'right' },
        ],
        variables: [
          { id: 'k', name: 'k', value: 3 },
          { id: 'window_sum', name: 'window_sum', value: 6 },
          { id: 'max_sum', name: 'max_sum', value: 9 },
          { id: 'i', name: 'i', value: 5 },
        ],
        calculations: [
          { id: 'calc-4', expression: '6 > 9', result: 'false', position: 'above' },
        ],
      },
    },
    {
      id: 'exec-12',
      phase: 'execution',
      explanation: 'Next iteration: i = 6. Loop condition: 6 < 6? No, exit loop.',
      codeLine: 5,
      decision: {
        question: 'Is i < len(nums)?',
        answer: '6 < 6 = false',
        action: 'Exit the for loop',
      },
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'dimmed' },
              { value: 1, index: 1, state: 'dimmed' },
              { value: 5, index: 2, state: 'dimmed' },
              { value: 1, index: 3, state: 'normal' },
              { value: 3, index: 4, state: 'normal' },
              { value: 2, index: 5, state: 'normal' },
            ],
          },
        ],
        pointers: [],
        variables: [
          { id: 'k', name: 'k', value: 3 },
          { id: 'window_sum', name: 'window_sum', value: 6 },
          { id: 'max_sum', name: 'max_sum', value: 9 },
          { id: 'i', name: 'i', value: 6, previousValue: 5 },
        ],
        calculations: [],
      },
    },
    {
      id: 'exec-13',
      phase: 'execution',
      explanation:
        'Return max_sum = 9. The maximum sum window is [5, 1, 3] at indices 2-4. Achieved in O(n) time!',
      codeLine: 9,
      dataState: {
        arrays: [
          {
            id: 'nums',
            elements: [
              { value: 2, index: 0, state: 'dimmed' },
              { value: 1, index: 1, state: 'dimmed' },
              { value: 5, index: 2, state: 'success' },
              { value: 1, index: 3, state: 'success' },
              { value: 3, index: 4, state: 'success' },
              { value: 2, index: 5, state: 'dimmed' },
            ],
          },
        ],
        pointers: [
          { id: 'left', name: 'left', index: 2, color: 'left' },
          { id: 'right', name: 'right', index: 4, color: 'right' },
        ],
        variables: [
          { id: 'k', name: 'k', value: 3 },
          { id: 'result', name: 'result', value: 9 },
        ],
        calculations: [],
      },
    },
  ],
};
