"""Reproduce the synthetic stock-availability example using only Python's standard library.
This is an illustrative demonstration, not company data or a production demand model.
Run: python generate_retail_demo.py (writes CSV and JSON alongside this file).
"""
import csv
import json
import math
import random
from pathlib import Path

SEED = 20261125
DAYS = 30
OUT = Path(__file__).resolve().parent
rng = random.Random(SEED)

def poisson(mean):
    threshold = math.exp(-mean)
    k, p = 0, 1.0
    while p > threshold:
        k += 1
        p *= rng.random()
    return k - 1

# Hypothetical rates and availability profiles deliberately include constrained, high-demand SKUs.
rows = []
for i in range(36):
    category = ('Fabric', 'Wallcovering', 'Trim')[i % 3]
    rate = round(rng.uniform(1.5, 8.0), 2)
    available = rng.randint(4, 12) if i % 5 == 0 else rng.randint(17, 30)
    # Availability is assigned independently of the daily Poisson arrivals conditional on the SKU.
    sold = sum(poisson(rate) for _ in range(available))
    rows.append({'sku': f'SKU-{i+1:02d}', 'category': category, 'available_days': available,
                 'period_days': DAYS, 'units_sold': sold, 'sales_per_available_day': round(sold/available, 4),
                 'simulated_demand_rate': rate})

with (OUT/'retail_demo.csv').open('w', newline='', encoding='utf-8') as f:
    writer = csv.DictWriter(f, fieldnames=list(rows[0])); writer.writeheader(); writer.writerows(rows)
(OUT/'retail_demo.json').write_text(json.dumps(rows, indent=2), encoding='utf-8')
