"""CLI modification pattern tests (targeted updates)."""

from __future__ import annotations

import pytest

from conftest import (
    build_excel_cli_eval,
    assert_cli_exit_codes,
    assert_regex,
    unique_path,
)

pytestmark = [pytest.mark.aitest, pytest.mark.copilot, pytest.mark.cli]


@pytest.mark.asyncio
async def test_cli_range_updates(copilot_eval, excel_cli_servers, excel_cli_skill_dir):
    agent = build_excel_cli_eval(
        "cli-range-updates",
        servers=excel_cli_servers,
        skill_dir=excel_cli_skill_dir,
        max_turns=20,
    )

    prompt = f"""
1. Create a new empty Excel file at {unique_path('llm-test-range-cli')} and open it
2. Set up a budget in A1:C4 on Sheet1:
   Row 1: Category, Budget, Actual
   Row 2: Rent, 1000, 1000
   Row 3: Food, 500, 450
   Row 4: Transport, 200, 180
3. Put the literal Excel formula =ROW()*1000+COLUMN() in cell D1 (it will calculate to 1004)
4. Update Food actual (C3) to 480
5. Update Transport actual (C4) to 195
6. Add a new row 5: Utilities, 150, 145
7. Read D1 to verify the formula still calculates to 1004
8. Read all data from A1:C5 to verify the updates
9. Close the file without saving
10. Summarize the values you found, especially D1 and the updated amounts.
"""
    result = await copilot_eval(agent, prompt)
    assert result.success
    assert_cli_exit_codes(result)
    # Loosen assertions - either 1004 appears or the formula was verified
    assert_regex(result.final_response, r"(?i)(1004|formula|d1|verified)")
    assert_regex(result.final_response, r"(?i)(480|food|updated)")


@pytest.mark.asyncio
async def test_cli_table_updates(copilot_eval, excel_cli_servers, excel_cli_skill_dir):
    agent = build_excel_cli_eval(
        "cli-table-updates",
        servers=excel_cli_servers,
        skill_dir=excel_cli_skill_dir,
        max_turns=20,
    )

    prompt = f"""
1. Create a new empty Excel file at {unique_path('llm-test-table-cli')} and open it
2. Put sales data in A1:C4 on Sheet1:
   Row 1: Product, Price, Quantity
   Row 2: Widget, 25, 3
   Row 3: Gadget, 50, 2
   Row 4: Device, 75, 1
3. Convert range A1:C4 into an Excel Table named "SalesTable"
4. Update Widget quantity (C2) from 3 to 5 - this should NOT delete the table
5. List tables to confirm exactly 1 table named "SalesTable" exists
6. Close the file without saving
"""
    result = await copilot_eval(agent, prompt)
    assert result.success
    assert_cli_exit_codes(result)
    assert_regex(result.final_response, r"(?i)(salestable)")


@pytest.mark.asyncio
async def test_cli_chart_updates(copilot_eval, excel_cli_servers, excel_cli_skill_dir):
    agent = build_excel_cli_eval(
        "cli-chart-updates",
        servers=excel_cli_servers,
        skill_dir=excel_cli_skill_dir,
        max_turns=20,
    )

    prompt = f"""
1. Create a new empty Excel file at {unique_path('llm-test-chart-cli')} and open it
2. Put chart data in A1:B4 on Sheet1:
   Row 1: Month, Sales
   Row 2: Jan, 100
   Row 3: Feb, 150
   Row 4: Mar, 200
3. Create a column chart from A1:B4 with title "Monthly Sales"
4. Change ONLY the chart title to "Q1 Sales Report" - do NOT delete and recreate the chart
5. List charts to confirm exactly 1 chart exists
6. Close the file without saving
"""
    result = await copilot_eval(agent, prompt)
    assert result.success
    assert_cli_exit_codes(result)
    assert_regex(result.final_response, r"(?i)(q1 sales report|chart)")


@pytest.mark.asyncio
async def test_cli_sheet_structural_changes(copilot_eval, excel_cli_servers, excel_cli_skill_dir):
    agent = build_excel_cli_eval(
        "cli-sheet-struct",
        servers=excel_cli_servers,
        skill_dir=excel_cli_skill_dir,
        max_turns=20,
    )

    prompt = f"""
1. Create a new empty Excel file at {unique_path('llm-test-struct-cli')} and open it
2. Put employee data in A1:C5 on Sheet1:
   Row 1: Name, Department, ID
   Row 2: Alice, Engineering, =ROW()*100 (formula shows 200)
   Row 3: Bob, Marketing, =ROW()*100 (formula shows 300)
   Row 4: Carol, Sales, =ROW()*100 (formula shows 400)
   Row 5: Dave, Support, =ROW()*100 (formula shows 500)
3. Delete the row with "Bob" (row 3), shifting remaining rows up
4. Rename the "Department" header (B1) to "Team"
5. Read all data including column C to verify:
   - Alice's ID formula now shows 200
   - Carol's ID formula now shows 300
   - Dave's ID formula now shows 400
   - Bob is gone
   - Header B1 says "Team"
6. Close the file without saving
"""
    result = await copilot_eval(agent, prompt)
    assert result.success
    assert_cli_exit_codes(result)
    assert_regex(result.final_response, r"(?i)(200)")
    assert_regex(result.final_response, r"(?i)(300)")
    assert_regex(result.final_response, r"(?i)(400)")
    assert_regex(result.final_response, r"(?i)(team)")
