"""
Create a practice expenses file.

This script writes data/expenses.csv next to this file. The file is
intentionally a little messy (mixed date formats, extra spaces, mixed
category names) so the summary script has something real to clean.

The rows are a fixed list, so every run writes the same file.
"""

import csv
from pathlib import Path

# Path(__file__) is "this Python file." .parent is the folder it lives in.
TUTORIAL_DIR = Path(__file__).resolve().parent
DEFAULT_CSV_PATH = TUTORIAL_DIR / "data" / "expenses.csv"

# Column names for the CSV file (CSV means "comma-separated values,"
# a plain-text table you can open in a spreadsheet).
COLUMNS = ["date", "merchant", "category", "notes", "amount"]

# Each inner list is one purchase. Values are strings on purpose so we
# can leave in extra spaces, "$", and different date styles.
EXPENSE_ROWS = [
    # January — mixed dates and categories
    ["2026-01-03", " Whole Foods ", "FOOD", "weekly groceries", " 64.18"],
    ["1/8/2026", "Uber", "transport", "ride downtown", "$12.50"],
    ["January 10, 2026", "Netflix", "STREAMING", "monthly plan", "15.99"],
    ["2026-01-12", "Blue Bottle", " coffee ", "latte", "6.75"],
    ["01/15/2026", "PG&E", "Electric", "January bill", " 92.40 "],
    ["Jan 18, 2026", "Shell", "GAS", "fill-up", "$48.20"],
    ["2026-01-20", "Trader Joe's", "groceries", "", "41.03"],
    ["1/22/2026", "AMC", "movies", "matinee", "14.00"],
    ["January 25, 2026", "  landlord  ", "Rent", "January rent", "1,450.00"],
    ["2026-01-28", "Comcast", "internet", "", "$79.99"],
    ["01-30-2026", "Chipotle", "dining", "burrito bowl", "13.45"],
    # February
    ["2026-02-02", "Safeway", "Food", "weekly groceries", "58.77"],
    ["2/5/2026", "Lyft", "UBER", "airport", " 34.10"],
    ["February 7, 2026", "Spotify", "Entertainment", "family plan", "16.99"],
    ["2026-02-09", "Starbucks", "COFFEE", "", "$5.45"],
    ["02/12/2026", "PG&E", "utilities", "February bill", "88.15"],
    ["Feb 14, 2026", "Chevron", "gas", "", "51.30"],
    ["2026-02-16", "Whole Foods", " GROCERIES ", "produce", "37.62"],
    ["2/19/2026", "Parking Garage", "parking", "city lot", "8.00"],
    ["February 21, 2026", "landlord", "HOUSING", "February rent", "1,450.00"],
    ["2026-02-24", "Comcast", "Internet", "", "79.99"],
    ["02-26-2026", "Thai Place", "RESTAURANT", "dinner", "$42.80"],
    ["2/28/2026", "CVS", "other", "toothpaste", "9.16"],
    # March
    ["2026-03-01", "Trader Joe's", "food", "", "55.40"],
    ["3/4/2026", "Uber", "Transport", "late ride", "$18.75"],
    ["March 6, 2026", "Netflix", "streaming", "", "15.99"],
    ["2026-03-09", "Blue Bottle", "Coffee", "two drinks", "13.50"],
    ["03/11/2026", "PG&E", "ELECTRIC", "March bill", " 85.00"],
    ["Mar 14, 2026", "Shell", "Gas", "", "44.90"],
    ["2026-03-17", "Safeway", "Groceries", "weekly groceries", "62.11"],
    ["3/20/2026", "AMC", "Movies", "", "16.50"],
    ["March 22, 2026", "landlord", "rent", "March rent", "1,450.00"],
    ["2026-03-25", "Comcast", "INTERNET", "", "79.99"],
    ["03-27-2026", "Chipotle", "Dining", "", "12.80"],
    ["3/29/2026", "Target", "Other", "household", "27.34"],
    # Rows the summary script should skip (bad date or blank amount)
    ["not-a-date", "Mystery Shop", "food", "broken date", "10.00"],
    ["2026-03-30", "Unknown", "food", "missing amount", ""],
]


def write_expenses_csv(csv_path):
    """Write the practice expense rows to csv_path.

    A path is the location of a file on your computer, like
    tutorials/01-expense-summary/data/expenses.csv.
    """
    csv_path = Path(csv_path)
    # mkdir creates the data folder if it is not there yet.
    csv_path.parent.mkdir(parents=True, exist_ok=True)

    with csv_path.open("w", newline="", encoding="utf-8") as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow(COLUMNS)
        writer.writerows(EXPENSE_ROWS)

    return csv_path


def main():
    saved_path = write_expenses_csv(DEFAULT_CSV_PATH)
    print(f"Wrote {len(EXPENSE_ROWS)} expense rows to {saved_path}")
    print("Some dates, categories, and amounts are messy on purpose.")


if __name__ == "__main__":
    main()
