#!/usr/bin/env python3
"""Import all historical complaints sequentially."""

import subprocess
import sys
import time


def run_import(cmd):
    """Run import command and return success count."""
    result = subprocess.run(cmd, shell=True, capture_output=True, text=True)

    success = 0
    processed = 0
    failed = 0

    for line in result.stdout.split("\n"):
        if "Successfully imported:" in line:
            success = int(line.split(":")[1].strip())
        elif "Total rows processed:" in line:
            processed = int(line.split(":")[1].strip())
        elif "Failed:" in line:
            failed = int(line.split(":")[1].strip())

    return success, processed, failed


# 2022
print("=== 2022 ===")
year = 2022
cmd_base = ".venv/bin/python manage.py import_historical_complaints"
file_path = f'"data/Complaints Report - {year}.xlsx"'
sheets = ["AUG 2022 ", "SEP 2022 ", "OCT 2022", "NOV 2022", "DEC 2022"]

total_2022 = 0
for sheet in sheets:
    cmd = f'{cmd_base} {file_path} --sheet="{sheet}"'
    success, processed, failed = run_import(cmd)
    total_2022 += success
    print(f"  {sheet}: {success}/{processed} (failed: {failed})")
    time.sleep(0.5)  # Brief pause between imports

print(f"2022 Total: {total_2022}")

# 2023
print("\n=== 2023 ===")
year = 2023
file_path = f'"data/Complaints Report - {year}.xlsx"'
sheets = [
    "January 2023 ",
    "February 2023",
    "March 2023",
    "April 2023 ",
    "May 2023",
    "June 2023",
    "July 2023",
    "August 2023",
    "September 2023",
    "October 2023",
    "November 2023",
    "December 2023",
]

total_2023 = 0
for sheet in sheets:
    cmd = f'{cmd_base} {file_path} --sheet="{sheet}"'
    success, processed, failed = run_import(cmd)
    total_2023 += success
    print(f"  {sheet}: {success}/{processed} (failed: {failed})")
    time.sleep(0.5)

print(f"2023 Total: {total_2023}")

# 2024
print("\n=== 2024 ===")
year = 2024
file_path = f'"data/Complaints Report - {year}.xlsx"'
sheets = [
    "January 2024",
    "February 2024",
    "March 2024 ",
    "April 2024",
    "May 2024",
    "June 2024",
    "July 2024",
    "August 2024",
    "September 2024",
    "October 2024",
    "November 2024",
    "December 2024",
]

total_2024 = 0
for sheet in sheets:
    cmd = f'{cmd_base} {file_path} --sheet="{sheet}"'
    success, processed, failed = run_import(cmd)
    total_2024 += success
    print(f"  {sheet}: {success}/{processed} (failed: {failed})")
    time.sleep(0.5)

print(f"2024 Total: {total_2024}")

# 2025
print("\n=== 2025 ===")
year = 2025
cmd_base = ".venv/bin/python manage.py import_2025_complaints_basic"
file_path = f'"data/Complaints Report - {year}.xlsx"'
sheets = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"]

total_2025 = 0
for sheet in sheets:
    cmd = f'{cmd_base} {file_path} --sheet="{sheet}"'
    success, processed, failed = run_import(cmd)
    total_2025 += success
    print(f"  {sheet}: {success}/{processed} (failed: {failed})")
    time.sleep(0.5)

print(f"2025 Total: {total_2025}")

print(f"\n=== GRAND TOTAL ===")
print(f"All years: {total_2022 + total_2023 + total_2024 + total_2025}")
