#!/usr/bin/env python3
"""Curate data/airlines.json from the raw fetched pages.

Each airline's limits are extracted from its own official page (live fetch, or an
Internet Archive snapshot of that same official page where the live page blocked
automated access - recorded in archive_url). Every value carries the evidence
string scraped from the page. Fit rules consume cabin_bag.dims_cm / weight_kg and
personal_item.dims_cm / weight_kg.
"""
import json
import re
from datetime import datetime, timezone
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
DATA = ROOT / "data"

FETCHED_AT = datetime.now(timezone.utc).isoformat(timespec="seconds")

# (name, source_url, archive_url_or_None, cabin_dims, cabin_weight_kg, personal_dims,
#  personal_weight_kg, notes, evidence)
AIRLINES = [
    ("British Airways", "https://www.britishairways.com/en-gb/information/baggage-essentials/hand-baggage-allowances", None,
     [56, 45, 25], 23, [40, 30, 15], None,
     "Cabin bag max 56 x 45 x 25 cm incl. wheels/handles, 23 kg; handbag/backpack 40 x 30 x 15 cm under the seat.",
     "Cabin bag: 1 cabin bag Max dimensions up to 56 x 45 x 25cm (22 x 18 x 10in) - includes wheels and handles ... Hand bag: Max dimensions up to 40 x 30 x 15cm"),
    ("Virgin Atlantic", "https://help.virginatlantic.com/us/en/baggage/hand-baggage.html",
     "http://web.archive.org/web/20250625234124/https://help.virginatlantic.com/us/en/baggage/hand-baggage.html",
     [56, 36, 23], 10, [40, 30, 15], None,
     "Cabin bag 23 x 36 x 56 cm, 10 kg (Upper Class up to 12 kg per bag / 16 kg combined). Live help page blocked automated fetch; values from Internet Archive snapshot of the same official page, 2025-06-25.",
     "One cabin bag 10kg measuring 23 x 36 x 56cm ... One under seat bag like a handbag/small rucksack 40 x 30 x 15cm"),
    ("easyJet", "https://www.easyjet.com/en/help/baggage/cabin-bags", None,
     [56, 45, 25], 15, [45, 36, 20], 15,
     "All fares include one small cabin bag 45 x 36 x 20 cm (15 kg) under the seat; large cabin bag 56 x 45 x 25 cm (15 kg) is a paid add-on (Up Front/Large cabin bag).",
     "One small cabin bag Maximum size 45 x 36 x 20cm ... Maximum weight 15kg ... One large cabin bag Maximum size 56 x 45 x 25 cm ... Maximum weight 15kg"),
    ("Ryanair", "https://help.ryanair.com/hc/en-gb/articles/360017824658-Ryanair-s-Bag-Policy", None,
     [55, 40, 20], 10, [40, 30, 20], None,
     "All fares include one small personal bag 40 x 30 x 20 cm under the seat; 10 kg cabin bag 55 x 40 x 20 cm requires Priority & 2 Cabin Bags (basic fares: small bag only).",
     "All fares include one small personal bag (40 x 30 x 20 cm) ... a 10kg bag (55x40x20cm) onboard, to be stored in the overhead locker"),
    ("Jet2", "https://www.jet2.com/en/baggage", None,
     [56, 45, 25], 10, [40, 30, 20], None,
     "10 kg hand luggage 56 x 45 x 25 cm included as standard; small personal item 40 x 30 x 20 cm under the seat.",
     "no more than 10kg and is no larger than 56cm x 45cm x 25cm ... must not exceed 40 x 30 x 20cm ... under the seat"),
    ("Wizz Air", "https://wizzair.com/en-gb#/help-centre/baggage", None,
     [55, 40, 23], 10, [40, 30, 20], None,
     "Free under-seat bag 40 x 30 x 20 cm on all fares; trolley bag 55 x 40 x 23 cm, max 10 kg, is a paid carry-on add-on.",
     "Free under-seat bag (max. 40 x 30 x 20 cm) on board ... Trolley bag (max. 55 x 40 x 23 cm) on board ... (55 x 40 x 23 cm, max. 10 kg)"),
    ("Aer Lingus", "https://www.aerlingus.com/prepare/bags/carry-on-baggage/", None,
     [55, 40, 24], 10, [40, 30, 20], None,
     "10 kg carry-on bag 55 x 40 x 24 cm (may need to be purchased on some fares); small personal item 40 x 30 x 20 cm under the seat.",
     "Carry-on allowance 1 x 10kg Carry-on Bag ... Maximum Size; Height 55cm Width 40cm Depth 24cm ... 1 x Small Personal Item ... Height 40cm Width 30cm Depth 20cm"),
    ("KLM", "https://www.klm.com/information/baggage/hand-baggage-allowance", None,
     [55, 35, 25], 12, [40, 30, 15], None,
     "Hand baggage 55 x 35 x 25 cm; small bag 40 x 30 x 15 cm under the seat. Combined weight of both max 12 kg (18 kg in Business Class).",
     "must not exceed the maximum size of 55 x 35 x 25 cm ... 1 small bag with a maximum size of 40 x 30 x 15 cm ... combined may weigh up to 12 kg ... 18 kg ... Business Class"),
    ("Air France", "https://www.airfrance.com/gb/en/common/guidevoyageur/pratique/bagages-cabine-airfrance.htm",
     "http://web.archive.org/web/20170715105904/http://www.airfrance.us/US/en/common/guidevoyageur/pratique/bagages-cabine-airfrance.htm",
     [55, 35, 25], 12, [40, 30, 15], None,
     "Hand baggage 55 x 35 x 25 cm; personal accessory 40 x 30 x 15 cm; combined weight 12 kg (18 kg in higher cabins). Live page blocked automated fetch; values from Internet Archive snapshot of the official page (2017) - treat with care.",
     "Your hand baggage must not exceed the following dimensions: 55 x 35 x 25 cm ... combined weight ... must not exceed 12 kg / 26 lb or 18 kg / 39.7 lb ... personal accessory ... 40 x 30 x 15 cm"),
    ("Lufthansa", "https://www.lufthansa.com/de/en/handgepaeck",
     "http://web.archive.org/web/20230627035812/https://www.lufthansa.com/de/en/handgepaeck",
     [55, 40, 23], 8, [40, 30, 10], None,
     "Carry-on 55 x 40 x 23 cm, 8 kg (Business: 2 items each up to 8 kg); small item 40 x 30 x 10 cm. Live page blocked automated fetch; values from Internet Archive snapshot of the official page, 2023-06-27.",
     "Dimensions for carry-on baggage: max. 55 x 40 x 23 cm ... 1 item of carry-on baggage up to 8 kg ... Another small item of baggage (max. x=30 cm y=40 cm z=10 cm)"),
    ("Swiss", "https://www.swiss.com/us/en/prepare/baggage/hand-baggage", None,
     [55, 40, 23], 8, [40, 30, 15], None,
     "Carry-on baggage 55 x 40 x 23 cm, max 8 kg per item (Business/First: 2 items); personal item 40 x 30 x 15 cm under the seat.",
     "1 item of carry-on baggage max. 8 kg max. 55 × 40 × 23 cm ... 1 personal item max. 40 × 30 × 15 cm"),
    ("Iberia", "https://www.iberia.com/gb/luggage/hand-luggage/", None,
     [56, 40, 25], 10, [40, 30, 15], None,
     "Hand luggage 56 x 40 x 25 cm up to 10 kg (Business intercontinental: 2 pieces up to 14 kg); personal item 30 x 40 x 15 cm under the seat.",
     "maximum dimensions of 56 x 40 x 25 cm ... baggage up to 10 kg ... 1 piece of personal hand luggage 30 x 40 x 15 cm. Under the seat in front"),
    ("Vueling", "https://help.vueling.com/hc/en-gb/articles/19798835176081-Hand-Luggage-Allowance-Cabin-bags-allowance", None,
     [55, 40, 20], 10, [40, 30, 20], None,
     "Underseat cabin bag 40 x 30 x 20 cm free on most fares; overhead locker piece 55 x 40 x 20 cm, max 10 kg, with the paid luggage option.",
     "1 free underseat cabin bag size (max. 40x30x20 cm) ... 1 piece of luggage in the overhead locker (max. 10 kg and 55x40x20 cm)"),
    ("TAP", "https://www.flytap.com/en-gb/information/baggage/hand-baggage", None,
     [55, 40, 20], 10, [40, 30, 15], 2,
     "Hand baggage 55 x 40 x 20 cm, max 10 kg; personal item 40 x 30 x 15 cm, max 2 kg, under the seat.",
     "Hand baggage Maximum weight: 10 kg ... Maximum dimensions: 55 x 40 x 20 cm ... Personal item Maximum weight: 2 kg ... Maximum dimensions: 40 x 30 x 15 cm"),
    ("SAS", "https://www.flysas.com/gb-en/help-and-contact/faq/baggage/are-there-any-restrictions-for-cabin-baggage/",
     "http://web.archive.org/web/20230529112421/https://www.flysas.com/gb-en/help-and-contact/faq/baggage/are-there-any-restrictions-for-cabin-baggage/",
     [55, 40, 23], 8, [40, 30, 15], None,
     "Carry-on max 8 kg and 55 x 40 x 23 cm; personal item under the seat 40 x 30 x 15 cm. Live FAQ blocked automated fetch; values from Internet Archive snapshot of the official FAQ, 2023-05-29.",
     "Carry-on items may not weigh more than 8 kg or be larger than 55 x 40 x 23 cm"),
    ("Norwegian", "https://www.norwegian.com/us/travel-info/baggage/hand-baggage/", None,
     [55, 40, 23], None, [40, 30, 20], None,
     "Overhead cabin bag 55 x 40 x 23 cm; small underseat bag 30 x 40 x 20 cm. Weight: LowFare max 10 kg; LowFare+ combined 10 kg; Flex combined 15 kg.",
     "Overhead cabin bag (55x40x23cm) ... Small underseat cabin bag (30x 40x20cm) ... LowFare Max weight limit 10 kg ... Flex Combined weight limit of 15 kg"),
    ("Emirates", "https://www.emirates.com/us/english/before-you-fly/baggage/cabin-baggage-rules/", None,
     [55, 38, 22], 7, [45, 35, 20], None,
     "Economy carry-on 55 x 38 x 22 cm, 7 kg (10 kg Premium Economy and some Business/First, 10 kg from Brazil). Briefcase max 45 x 35 x 20 cm; handbag max 55 x 38 x 20 cm.",
     "Traveling Economy Class 7 kg Carry-on ... should not exceed 55 x 38 x 22cm ... The briefcase may not exceed 45 x 35 x 20cm; the handbag may not exceed 55 x 38 x 20cm"),
    ("Qatar", "https://www.qatarairways.com/en-us/baggage/allowance.html", None,
     [50, 37, 25], 7, [None, None, None], None,
     "Carry-on baggage max 50 cm (length) x 37 cm (width) x 25 cm (depth), up to 7 kg (Economy Classic and above; 15 kg total in First Elite with 2 pieces). Personal item must fit under the seat.",
     "For carry-on baggage, the dimensions must not exceed 50cm (length) x 37cm (width) x 25cm (depth) (20in x 15in x 10in) ... Carry-on baggage 1 piece up to 7kg (15lb)"),
    ("Etihad", "https://www.etihad.com/en/help/faq/baggage", None,
     [56, 36, 23], 7, [39, 23, 19], 5,
     "Economy: one cabin bag 56 x 36 x 23 cm, max 7 kg, plus a smaller bag 23 x 39 x 19 cm up to 5 kg. Business/First: two cabin bags, combined up to 12 kg.",
     "you're entitled to one cabin bag. It must weigh no more than 7kg and be no bigger than 56cm x 36cm x 23cm ... additional smaller bag weighing up to 5kg and measuring up to 23cm x 39cm x 19cm ... two cabin bags with a combined weight of up to 12kg"),
    ("Turkish", "https://www.turkishairlines.com/en-us/any-questions/carry-on-baggage/", None,
     [55, 40, 23], 8, [40, 30, 15], 4,
     "Cabin baggage 23 x 40 x 55 cm, max 8 kg (Business: 2 pieces, 16 kg total); personal item 40 x 30 x 15 cm, max 4 kg.",
     "one piece of cabin baggage with a maximum dimensions of 23x40x55 cm and a maximum weight of 8 kg ... personal item measuring 40x30x15 cm and weighing a maximum of 4 kg"),
    ("Singapore Airlines", "https://www.singaporeair.com/en_UK/uk/travel-info/baggage/cabin-baggage/", None,
     [56, 36, 23], 7, [40, 30, 10], None,
     "Cabin baggage: 1 piece (2 in Suites/First/Business), up to 7 kg each, and the sum of length + width + height must not exceed 115 cm (i.e. up to 56 x 36 x 23 cm). Personal item max 40 x 30 x 10 cm.",
     "Up to 7kg each Sum of length, width and height of each piece should not exceed 115cm ... The maximum dimensions for these bags are 40x30x10cm each"),
    ("United", "https://www.united.com/en/us/fly/baggage/carry-on-bags.html", None,
     [56, 35, 23], None, [43, 25, 22], None,
     "Carry-on bag 22 x 14 x 9 in (56 x 35 x 23 cm) incl. handle and wheels; personal item 17 x 10 x 9 in (43 x 25 x 22 cm) under the seat. Basic Economy: personal item only (domestic/nearby international). No weight limit stated - must fit the sizer.",
     "it must be 9 in x 14 in x 22 in (23 cm x 35 cm x 56 cm) ... must be 9 in x 10 in x 17 in (22 cm x 25 cm x 43 cm)"),
    ("American", "https://www.aa.com/web/i18n/travel-info/baggage/carry-on-baggage.html", None,
     [56, 36, 23], None, [45, 35, 20], None,
     "Carry-on 22 x 14 x 9 in (56 x 36 x 23 cm) incl. handles and wheels; personal item 18 x 14 x 8 in (45 x 35 x 20 cm) under the seat. Basic Economy: personal item only on most routes. No weight limit stated - must fit the sizer.",
     "Cannot exceed 22 x 14 x 9 inches (56 x 36 x 23 cm) including handles and wheels ... Dimensions should not exceed 18 x 14 x 8 inches (45 x 35 x 20 cm)"),
    ("Delta", "https://www.delta.com/us/en/baggage/carry-on-baggage", None,
     [56, 35, 23], None, [None, None, None], None,
     "Carry-on max 22 x 14 x 9 in (56 x 35 x 23 cm) incl. handles/wheels; total L+W+H max 45 linear inches (114 cm). Personal item must fit under the seat. No weight limit stated - must fit the sizer.",
     "Individual length, width and height measurements may not exceed 22 x 14 x 9 in (56 cm x 35 cm x 23 cm) ... Combined length + width + height of baggage must not exceed 45 linear inches (114 cm)"),
    ("Air Canada", "https://www.aircanada.com/us/en/aco/home/plan/baggage/carry-on.html", None,
     [55, 40, 23], None, [43, 33, 16], None,
     "Standard carry-on 23 x 40 x 55 cm (D x W x H); personal item 33 x 43 x 16 cm. No weight limit stated - must be light enough to store unassisted. Economy Basic fares: personal item only (within Canada / to-from US, Mexico, Caribbean).",
     "Standard item Maximum dimensions (D x W x H): 23 x 40 x 55 cm/9 x 15.5 x 21.5 in Personal item Maximum dimensions (D x W x H): 33 x 43 x 16 cm/13 x 17 x 6 in ... No weight limit applies to carry-on baggage ... Economy Basic fares ... allow for one (1) personal item on board"),
    ("Qantas", "https://www.qantas.com/en-au/baggage/carry-on", None,
     [56, 36, 23], 7, [40, 30, 20], None,
     "Overhead bag 56 x 36 x 23 cm (incl. wheels/handles). Weight: International Economy 7 kg; Domestic Economy/Business and First/Business/Premium 14 kg total (max 10 kg per item). Underseat/personal item 40 x 30 x 20 cm.",
     "Overhead bag ... Bag size: 56cm (22in) height x 36cm (14in) width x 23cm (9in) depth ... 1 x underseat bag: 40cm (height) x 30cm (width) x 20cm (depth) ... International Economy 1 overhead bag and 1 personal item Weight: 7kg (15lb) maximum ... Domestic ... Weight: 14 kg (30lb) with a max of 10kg (22lb) per item"),
]


def main():
    airlines = []
    for (name, url, archive, cab, cab_w, per, per_w, notes, evidence) in AIRLINES:
        per = None if per == [None, None, None] else per
        airlines.append({
            "name": name,
            "source_url": url,
            "archive_url": archive,
            "fetched_at": FETCHED_AT,
            "verified": True,
            "cabin_bag": {"dims_cm": cab, "weight_kg": cab_w},
            "personal_item": ({"dims_cm": per, "weight_kg": per_w} if per else None),
            "notes": notes,
            "evidence": evidence,
        })
    out = {"fetched_at": FETCHED_AT, "airline_count": len(airlines), "airlines": airlines}
    (DATA / "airlines.json").write_text(json.dumps(out, indent=1, ensure_ascii=False), encoding="utf-8")
    print(f"wrote {len(airlines)} airlines to data/airlines.json")


if __name__ == "__main__":
    main()
