#!/usr/bin/env python3
import csv
import io
import sys
from pathlib import Path

BASE = Path(__file__).resolve().parent
MANIFEST = BASE / "apld_forecast_actual_manifest.csv"
ROWS_OUT = BASE / "apld_forecast_actual_rows.csv"

FIELDS = [
    "company", "period", "metric_name", "raw_value", "normalized_value", "unit",
    "value_qualifier", "ownership_basis", "forecast_or_actual", "is_derived",
    "source_url", "source_location", "source_snippet", "snippet_support_status",
    "confidence", "caveat",
]

ROW_SPECS = """source_id,company,period,metric_name,raw_value,normalized_value,unit,value_qualifier,ownership_basis,forecast_or_actual,is_derived,source_location,source_snippet,snippet_support_status,confidence,caveat
APLD_2025_06_02_PF1_COREWEAVE,Applied Digital,FY2025Q4,contracted_critical_it_load,250,250,MW,exact,critical_it_load_net_leased,forecast,False,PDF page 0 lines 4-11,Applied Digital has entered into two approximately 15-year lease agreements with CoreWeave; Applied Digital will deliver 250 megawatts MW of critical IT load,exact,high,Signed lease commitment; not live capacity.
APLD_2025_06_02_PF1_COREWEAVE,Applied Digital,FY2025Q4,optional_critical_it_load,150,150,MW,exact,critical_it_load_customer_option,forecast,False,PDF page 0 lines 13-15,CoreWeave also retains the option to access an additional 150 MW of critical IT load at Ellendale,exact,high,Customer option only at source date.
APLD_2025_06_02_PF1_COREWEAVE,Applied Digital,FY2025Q4,contracted_revenue,7,7,USD_billions,approximate,lease_base_term,forecast,False,PDF page 0 lines 8-12,Over the approximately 15-year term Applied Digital anticipates generating approximately $7 billion in total revenue from the leases,exact,high,Prospective contracted revenue; not recognized revenue.
APLD_2025_07_30_FY2025Q4,Applied Digital,FY2025Q4,operating_hosting_capacity,106+180,286,MW,exact,legacy_data_center_hosting_gross,actual,True,Data Center Hosting Update lines 105-108,As of May 31 2025 the Companys 106 MW facility in Jamestown ND and 180 MW facility in Ellendale ND are operating at full capacity,partial,high,Derived sum; legacy crypto/data-center hosting not AI Factory HPC capacity.
APLD_2025_07_30_FY2025Q4,Applied Digital,FY2025Q4,incremental_leased_capacity_option_exercised,150,150,MW,exact,critical_it_load_option_exercised_pending_lease,forecast,False,Recent Highlights lines 86-93,CoreWeave exercised its option for an additional 150MW which would bring the total capacity leased by CoreWeave to 400MW,exact,high,Option exercised after quarter-end; release says parties were finalizing lease.
APLD_2025_07_30_FY2025Q4,Applied Digital,FY2025Q4,planned_ready_for_service_capacity,100,100,MW,exact,critical_it_load_building_forecast,forecast,False,Management Commentary line 95,With the first 100 MW facility scheduled to be operational in Q4 2025,exact,high,Forecasted commencement window for first PF1 building.
APLD_2025_07_30_FY2025Q4,Applied Digital,FY2025Q4,planned_ready_for_service_capacity,150,150,MW,exact,critical_it_load_building_forecast,forecast,False,Management Commentary line 95,a second 150 MW facility scheduled to come online in mid-2026,exact,high,Forecasted commencement window for second PF1 building.
APLD_2025_07_30_FY2025Q4,Applied Digital,FY2025Q4,pro_forma_contracted_revenue,11,11,USD_billions,approximate,lease_base_term_after_option,forecast,False,Management Commentary lines 91-93,additional 150 MW would bring the total leased capacity to 400 MW and add approximately $4 billion which would bring total contracted revenue to approximately $11 billion,partial,high,Pro forma on prompt execution of third lease.
APLD_2025_08_18_PF2_GROUNDBREAK,Applied Digital,FY2026Q1,planned_campus_capacity,280,280,MW,exact,campus_design_capacity,forecast,False,PDF project announcement body,Polaris Forge 2 is designed for 280MW with initial capacity in 2026 and reaching full capacity in early 2027,partial,high,Campus design capacity; later lease disclosure uses 200 MW IT load.
APLD_2025_10_09_FY2026Q1,Applied Digital,FY2026Q1,contracted_critical_it_load,400,400,MW,exact,critical_it_load_net_leased,actual,False,Management Commentary lines 77-87,signed an additional 150 MW lease bringing the full 400 MW of critical IT load under contract with CoreWeave,exact,high,Contracted capacity; not all operating.
APLD_2025_10_09_FY2026Q1,Applied Digital,FY2026Q1,planned_initial_it_load,200,200,MW,approximate,critical_it_load_initial_phase,forecast,False,Subsequent to Quarter lines 82-83,approximately $3 billion 300 MW IT load AI Factory campus; initial 200 MW is expected to begin to come online in 2026 and reach full capacity in 2027,partial,medium,Release mixes 300 MW project language with 200 MW initial phase.
APLD_2025_10_09_FY2026Q1,Applied Digital,FY2026Q1,possible_expansion_capacity,1,1000,MW,greater_than_or_equal,critical_it_load_or_campus_capacity_conditional_power,forecast,False,Management Commentary lines 87-90,additional power allocations expected to push capacity beyond 1 gigawatt GW starting in 2028 to 2030,partial,medium,Normalized 1 GW to 1000 MW; conditional on future power allocations.
APLD_2025_10_09_FY2026Q1,Applied Digital,FY2026Q1,contracted_revenue,11,11,USD_billions,approximate,lease_base_term,actual,False,Management Commentary line 87,securing approximately $11 billion in prospective lease revenue over the approximately 15 year terms,exact,high,Prospective lease revenue; not recognized revenue.
APLD_2025_10_09_FY2026Q1,Applied Digital,FY2026Q1,project_development_cost,3,3,USD_billions,approximate,project_cost_estimate,forecast,False,Subsequent to Quarter line 82,approximately $3 billion 300 MW IT load AI Factory campus near Harwood North Dakota,exact,medium,Development cost estimate; not direct capex actual.
APLD_2025_10_09_FY2026Q1,Applied Digital,FY2026Q1,property_and_equipment_built_and_funded,1.6,1.6,USD_billions,greater_than,property_and_equipment_company_statement,actual,False,Management Commentary line 92,weve now built and funded over $1.6 billion in property and equipment,exact,medium,Company statement of built/funded PPE; not quarter-only capex.
APLD_2025_10_27_PF1_RFS50,Applied Digital,FY2026Q2,ready_for_service_capacity,50,50,MW,exact,critical_it_load_rfs_phase,actual,False,Body lines 54-60,achieved successful and on-time Ready for Service RFS for the first phase 50 MW at the first 100 megawatt MW building,exact,high,First 50 MW RFS; partial completion of 100 MW building.
APLD_2025_11_24_PF1_RFS100,Applied Digital,FY2026Q2,ready_for_service_capacity,100,100,MW,exact,critical_it_load_rfs_building,actual,False,Body lines 54-59,Building 1 now operates at its full 100 MW critical IT load marking the completion of the first of three contracted buildings,exact,high,Actual delivery against Q4 2025 forecast.
APLD_2026_01_07_FY2026Q2,Applied Digital,FY2026Q2,operating_hpc_capacity,100,100,MW,exact,critical_it_load_operating,actual,False,HPC Hosting Update lines 104-111,Operations commenced at our first HPC data center at Polaris Forge 1 with 100 MW of capacity,exact,high,Operating capacity snapshot at FY2026 Q2.
APLD_2026_01_07_FY2026Q2,Applied Digital,FY2026Q2,under_construction_capacity,150,150,MW,exact,critical_it_load_under_construction,actual,False,HPC Hosting Update line 108,A second 150 MW HPC data center is under construction at the same campus and is expected to come online in calendar 2026,exact,high,Construction status plus forecast timing.
APLD_2026_01_07_FY2026Q2,Applied Digital,FY2026Q2,planned_ready_for_service_capacity,150,150,MW,exact,critical_it_load_building_forecast,forecast,False,HPC Hosting Update line 108,a third 150 MW facility is anticipated in calendar 2027,exact,high,Future third PF1 building.
APLD_2026_01_07_FY2026Q2,Applied Digital,FY2026Q2,contracted_critical_it_load,200,200,MW,approximate,critical_it_load_net_leased,actual,False,HPC Hosting Update line 109,signed an approximately 15-year lease with a U.S. based investment-grade hyperscaler for 200 MW at Polaris Forge 2,exact,high,Contracted 200 MW at PF2; delivery still future.
APLD_2026_01_07_FY2026Q2,Applied Digital,FY2026Q2,operating_hosting_capacity,106+180,286,MW,exact,legacy_data_center_hosting_gross,actual,True,Data Center Hosting Update lines 112-115,As of November 30 2025 the Companys 106 MW facility in Jamestown ND and 180 MW facility in Ellendale ND are operating at full capacity,partial,high,Derived sum; legacy hosting not AI Factory HPC.
APLD_2026_01_07_FY2026Q2,Applied Digital,FY2026Q2,total_leased_capacity,600,600,MW,exact,critical_it_load_net_leased_portfolio,actual,False,Management Commentary lines 88-89,Collectively these lease agreements bring total leased capacity to 600 MW and aggregate prospective lease revenue to approximately $16 billion,partial,high,Portfolio contracted/leased capacity; not all operating.
APLD_2026_01_07_FY2026Q2,Applied Digital,FY2026Q2,total_prospective_lease_revenue,16,16,USD_billions,approximate,lease_base_term_portfolio,actual,False,Management Commentary lines 88-89,total leased capacity to 600 MW and aggregate prospective lease revenue to approximately $16 billion before exercise of any renewal options,exact,high,Prospective lease revenue; excludes renewal options.
APLD_2026_01_07_FY2026Q2,Applied Digital,FY2026Q2,partial_quarter_lease_revenue,12.0,12.0,USD_millions,approximate,recognized_hpc_lease_revenue,actual,False,Management Commentary lines 90-91,Applied Digital recognized $12.0 million in partial-quarter lease revenue,exact,high,Revenue conversion evidence for first operating building.
APLD_2026_01_07_FY2026Q2,Applied Digital,FY2026Q2,customer_payment,85.0,85.0,USD_millions,approximate,customer_payment_including_fit_out,actual,False,Management Commentary lines 90-91,CoreWeave paid approximately $85.0 million this quarter including $73.0 million for tenant fit-out activities,exact,medium,Customer payment includes tenant fit-out; not pure lease revenue.
APLD_2026_01_22_DF1_GROUNDBREAK,Applied Digital,FY2026Q3,planned_utility_power,430,430,MW,exact,gross_utility_power,forecast,False,PDF project announcement body,support an initial 430 MW of total utility power enabling up to 300 MW of critical IT load,partial,high,Utility power not critical IT load.
APLD_2026_01_22_DF1_GROUNDBREAK,Applied Digital,FY2026Q3,planned_critical_it_load,300,300,MW,up_to,critical_it_load_design_capacity,forecast,False,PDF project announcement body,support an initial 430 MW of total utility power enabling up to 300 MW of critical IT load,exact,high,Forecast; not yet leased at January source date.
APLD_2026_04_08_FY2026Q3,Applied Digital,FY2026Q3,under_construction_capacity,150+150,300,MW,exact,critical_it_load_under_construction,actual,True,Management Commentary lines 95-96,continuing to advance the next two 150 MW facilities in parallel,partial,high,Derived sum of two 150 MW PF1 buildings.
APLD_2026_04_08_FY2026Q3,Applied Digital,FY2026Q3,construction_pipeline_capacity,200,200,MW,exact,critical_it_load_construction_pipeline,actual,False,Management Commentary line 96,At Polaris Forge 2 the 200 MW investment-grade hyperscaler campus both buildings are progressing well,exact,high,Construction/pipeline snapshot.
APLD_2026_04_08_FY2026Q3,Applied Digital,FY2026Q3,grid_power_capacity_marketed,1,1000,MW,approximate,gross_grid_power_pipeline,forecast,False,Management Commentary lines 98-99,total grid power capacity across these locations is approximately 1 GW,partial,medium,Normalized 1 GW to 1000 MW; marketing pipeline subject to approvals.
APLD_2026_04_23_DF1_LEASE,Applied Digital,FY2026Q3,contracted_critical_it_load,300,300,MW,exact,critical_it_load_net_leased,actual,False,PDF page 0 lines 10-14,lease agreement with a new U.S. based high investment-grade hyperscaler; covers 300 megawatts MW of critical IT load,exact,high,Delta Forge 1 lease signed; operations future.
APLD_2026_04_23_DF1_LEASE,Applied Digital,FY2026Q3,contracted_revenue,7.5,7.5,USD_billions,approximate,lease_base_term,actual,False,PDF page 0 lines 12-18,This lease represents approximately $7.5 billion in total contracted value over an estimated 15-year lease term,exact,high,Contracted value; not recognized revenue.
APLD_2026_05_20_PF3_LEASE,Applied Digital,FY2026Q4,contracted_critical_it_load,300,300,MW,exact,critical_it_load_net_leased,actual,False,Transaction highlights lines 58-66,The agreement is for Polaris Forge 3 and designed to deliver 300 MW of critical IT load; 300 MW of critical IT load,exact,high,PF3 lease signed; initial operations future.
APLD_2026_05_20_PF3_LEASE,Applied Digital,FY2026Q4,secured_utility_power,430,430,MW,approximate,gross_grid_connected_utility_power,actual,False,Body line 59,designed to deliver 300 MW of critical IT load supported by approximately 430 MW of grid-connected utility power,exact,high,Utility power basis differs from critical IT load.
APLD_2026_05_20_PF3_LEASE,Applied Digital,FY2026Q4,contracted_revenue,7.5,7.5,USD_billions,approximate,lease_base_term,actual,False,Transaction highlights lines 60-67,15-year take-or-pay leases valued at approximately $7.5 billion in base-term contracted revenue,exact,high,Base-term contracted revenue; not recognized revenue.
APLD_2026_05_20_PF3_LEASE,Applied Digital,FY2026Q4,total_contracted_capacity,1200,1200,MW,exact,critical_it_load_net_portfolio,actual,False,Transaction highlights line 65,Total contracted capacity across four AI Factory campuses now reaches 1,200 MW of critical IT load net,exact,high,Portfolio contracted critical IT load; not delivered operating capacity.
APLD_2026_05_20_PF3_LEASE,Applied Digital,FY2026Q4,total_utility_power,1670,1670,MW,approximate,gross_utility_power_portfolio,actual,False,Transaction highlights line 65,total contracted capacity across four AI Factory campuses now reaches 1,200 MW of critical IT load net and approximately 1,670 MW of utility power gross,exact,high,Gross utility power; separate from critical IT load.
APLD_2026_05_20_PF3_LEASE,Applied Digital,FY2026Q4,expected_commencement_window,2027-08,2027.67,year,exact,initial_operations_commencement,forecast,False,Body line 73,Initial operations at Polaris Forge 3 are anticipated to commence in August 2027,exact,medium,Normalized to decimal year only for sorting; preserve raw date.
"""

def load_manifest():
    if not MANIFEST.exists():
        raise FileNotFoundError(f"missing manifest: {MANIFEST}")
    with MANIFEST.open(newline="", encoding="utf-8") as f:
        rows = list(csv.DictReader(f))
    by_id = {row["source_id"]: row for row in rows}
    if len(by_id) != len(rows):
        raise ValueError("duplicate source_id in manifest")
    return by_id

def build_rows():
    manifest = load_manifest()
    specs = list(csv.DictReader(io.StringIO(ROW_SPECS)))
    out = []
    for spec in specs:
        source_id = spec.pop("source_id")
        if source_id not in manifest:
            raise KeyError(f"row references missing source_id: {source_id}")
        spec["source_url"] = manifest[source_id]["source_url"]
        out.append({field: spec.get(field, "") for field in FIELDS})
    return out

def main():
    rows = build_rows()
    with ROWS_OUT.open("w", newline="", encoding="utf-8") as f:
        writer = csv.DictWriter(f, fieldnames=FIELDS)
        writer.writeheader()
        writer.writerows(rows)
    print(f"wrote {len(rows)} rows to {ROWS_OUT}")
    return 0

if __name__ == "__main__":
    sys.exit(main())
