from sheets.models import MonthlySheet, Client, Cell

TOP_RIGHT_CLIENTS = [
    "Dr. Fohrer",      # L
    "AG Buntk.",       # M
    "AG Alff",         # N
    "AG Gutfl.",       # O
    "M3 Thiele",       # P
    "M3 Buntkowsky",   # Q
    "M3 Gutfleisch",   # R
]

ROW_COUNT = 16  # top_right rows 0–15

sheets = MonthlySheet.objects.all().order_by('year', 'month')

for sheet in sheets:
    print(f"Fixing sheet {sheet.year}-{sheet.month}...")
    for col_idx, name in enumerate(TOP_RIGHT_CLIENTS):
        try:
            client = Client.objects.get(name=name)
        except Client.DoesNotExist:
            print(f"  WARNING: client {name!r} not found, skipping this column")
            continue

        for row_idx in range(ROW_COUNT):
            cell, created = Cell.objects.get_or_create(
                sheet=sheet,
                table_type='top_right',
                client=client,
                row_index=row_idx,
                column_index=col_idx,
                defaults={'value': None},
            )
            if created:
                print(f"    created cell: {name} row {row_idx}")
