"""Create fictional hourly retail demand; no model predictions or external data.

Usage: python generate_example.py --output demand.csv
Times are a fixed local hourly grid without daylight-saving transitions.
The school break and shutdown are fictional, repeated planning assumptions.
"""

import argparse
import csv
from datetime import datetime, timedelta
import math
from pathlib import Path

ORIGIN = datetime(2026, 10, 20)
HORIZON = 168
START = datetime(2023, 10, 20)


def make_records(start=START, origin=ORIGIN, horizon=HORIZON):
    end = origin + timedelta(hours=horizon)
    stamp = start
    rows = []
    while stamp < end:
        elapsed = (stamp - start).total_seconds() / 3600
        hour = 2 * math.pi * stamp.hour / 24
        week = 2 * math.pi * (stamp.weekday() + stamp.hour / 24) / 7
        year = 2 * math.pi * (stamp.timetuple().tm_yday - 1 + stamp.hour / 24) / 365.25
        promotion = int(23 <= stamp.day <= 25)
        school_vacation = int(stamp.month == 10 and stamp.day >= 20)
        public_holiday = int((stamp.month, stamp.day) in {(1, 1), (12, 25)})
        is_closed = int((stamp.month, stamp.day) == (10, 26))
        baseline = 100 + 18 * math.sin(hour - 1) + 12 * math.cos(week) + 16 * math.sin(year)
        noise = 2 * math.sin(elapsed * 1.71) + math.cos(elapsed * 0.43)
        y = max(0, baseline + 25 * promotion + 12 * school_vacation - 20 * public_holiday + noise)
        rows.append({
            "ds": stamp.isoformat(sep=" "),
            "y": round(0 if is_closed else y, 3),
            "baseline": round(baseline, 3),
            "promotion": promotion,
            "school_vacation": school_vacation,
            "public_holiday": public_holiday,
            "is_closed": is_closed,
            "hour_sin": round(math.sin(hour), 6),
            "hour_cos": round(math.cos(hour), 6),
            "week_sin": round(math.sin(week), 6),
            "week_cos": round(math.cos(week), 6),
            "year_sin": round(math.sin(year), 6),
            "year_cos": round(math.cos(year), 6),
        })
        stamp += timedelta(hours=1)
    return rows


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--output", type=Path, default=Path("demand.csv"))
    args = parser.parse_args()
    rows = make_records()
    with args.output.open("w", newline="", encoding="utf-8") as stream:
        writer = csv.DictWriter(stream, fieldnames=rows[0].keys())
        writer.writeheader()
        writer.writerows(rows)
    print(f"Wrote {len(rows)} fictional hourly observations to {args.output}")
