#!/usr/bin/env python3
# Copyright © Albert Lane. All Rights Reserved.
# Proprietary and Confidential — Albert Lane Proprietary Software License.
# Unauthorized use, copying, modification, or distribution is prohibited.
# License inquiries: https://albertlane.net
""" Automated FOIA Generator for 21 Flagged Counties """
import os
import json
from datetime import datetime

COUNTIES = [
  {
    "county": "Multnomah County",
    "state": "OR",
    "elections_email": "elections@multco.us",
    "records_officer": "Multnomah County Public Records Officer",
    "statute": "ORS 192.311 - 192.478",
    "z_score": 2.1,
    "ricci_curvature": 20.0
  },
  {
    "county": "Lane County",
    "state": "OR",
    "elections_email": "elections@lanecountyor.gov",
    "records_officer": "Lane County Public Records Coordinator",
    "statute": "ORS 192.311 - 192.478",
    "z_score": 1.92,
    "ricci_curvature": 19.54
  },
  {
    "county": "Los Angeles County",
    "state": "CA",
    "elections_email": "voterinfo@rrcc.lacounty.gov",
    "records_officer": "LA Registrar-Recorder/County Clerk PRA Unit",
    "statute": "California Public Records Act (Gov. Code \u00a7 7920.000 et seq.)",
    "z_score": 1.85,
    "ricci_curvature": 14.99
  },
  {
    "county": "Josephine County",
    "state": "OR",
    "elections_email": "elections@josephinecounty.gov",
    "records_officer": "Josephine County Clerk Elections Division",
    "statute": "ORS 192.311 - 192.478",
    "z_score": 1.72,
    "ricci_curvature": 18.73
  },
  {
    "county": "Klamath County",
    "state": "OR",
    "elections_email": "vpeters@klamathcounty.org",
    "records_officer": "Klamath County Clerk Elections Office",
    "statute": "ORS 192.311 - 192.478",
    "z_score": 1.64,
    "ricci_curvature": 17.96
  },
  {
    "county": "San Francisco County",
    "state": "CA",
    "elections_email": "sfvote@sfgov.org",
    "records_officer": "SF Department of Elections Sunshine Ordinance Officer",
    "statute": "California Public Records Act & SF Sunshine Ordinance",
    "z_score": 1.58,
    "ricci_curvature": 15.28
  },
  {
    "county": "Alameda County",
    "state": "CA",
    "elections_email": "elections@acgov.org",
    "records_officer": "Alameda County Registrar of Voters PRA Division",
    "statute": "California Public Records Act (Gov. Code \u00a7 7920.000 et seq.)",
    "z_score": 1.32,
    "ricci_curvature": 13.81
  },
  {
    "county": "Lincoln County",
    "state": "OR",
    "elections_email": "countyclerk@co.lincoln.or.us",
    "records_officer": "Lincoln County Elections Division",
    "statute": "ORS 192.311 - 192.478",
    "z_score": 1.25,
    "ricci_curvature": 17.82
  },
  {
    "county": "Jefferson County",
    "state": "OR",
    "elections_email": "elections@co.jefferson.or.us",
    "records_officer": "Jefferson County Clerk",
    "statute": "ORS 192.311 - 192.478",
    "z_score": 1.24,
    "ricci_curvature": 15.65
  },
  {
    "county": "Jackson County",
    "state": "OR",
    "elections_email": "elections@jacksoncountyor.gov",
    "records_officer": "Jackson County Elections Division",
    "statute": "ORS 192.311 - 192.478",
    "z_score": 1.2,
    "ricci_curvature": 16.07
  },
  {
    "county": "Malheur County",
    "state": "OR",
    "elections_email": "countyclerk@malheurco.org",
    "records_officer": "Malheur County Clerk",
    "statute": "ORS 192.311 - 192.478",
    "z_score": 1.13,
    "ricci_curvature": 13.67
  },
  {
    "county": "Wasco County",
    "state": "OR",
    "elections_email": "towner@co.wasco.or.us",
    "records_officer": "Wasco County Clerk & Elections Officer",
    "statute": "ORS 192.311 - 192.478",
    "z_score": 1.19,
    "ricci_curvature": 15.08
  },
  {
    "county": "Umatilla County",
    "state": "OR",
    "elections_email": "elections@umatillacounty.gov",
    "records_officer": "Umatilla County Elections Division",
    "statute": "ORS 192.311 - 192.478",
    "z_score": 0.8,
    "ricci_curvature": 13.95
  },
  {
    "county": "King County",
    "state": "WA",
    "elections_email": "elections@kingcounty.gov",
    "records_officer": "King County Elections Public Disclosure Officer",
    "statute": "Washington Public Records Act (RCW 42.56)",
    "z_score": 0.85,
    "ricci_curvature": 12.06
  },
  {
    "county": "Denver County",
    "state": "CO",
    "elections_email": "electionsro@denvergov.org",
    "records_officer": "Denver Elections Division CORA Officer",
    "statute": "Colorado Open Records Act (CORA, C.R.S. \u00a7 24-72-201 et seq.)",
    "z_score": 1.05,
    "ricci_curvature": 13.23
  },
  {
    "county": "Clark County",
    "state": "NV",
    "elections_email": "elinfo@clarkcountynv.gov",
    "records_officer": "Clark County Election Department Records Officer",
    "statute": "Nevada Public Records Act (NRS Chapter 239)",
    "z_score": 0.75,
    "ricci_curvature": 12.38
  },
  {
    "county": "Maricopa County",
    "state": "AZ",
    "elections_email": "elections@risc.maricopa.gov",
    "records_officer": "Maricopa County Elections Public Records Custodian",
    "statute": "Arizona Public Records Law (A.R.S. \u00a7 39-121 et seq.)",
    "z_score": 0.32,
    "ricci_curvature": 9.93
  },
  {
    "county": "Clackamas County",
    "state": "OR",
    "elections_email": "elections@clackamas.us",
    "records_officer": "Clackamas County Clerk Elections Division",
    "statute": "ORS 192.311 - 192.478",
    "z_score": 1.05,
    "ricci_curvature": 14.12
  },
  {
    "county": "Deschutes County",
    "state": "OR",
    "elections_email": "elections@deschutes.org",
    "records_officer": "Deschutes County Clerk Elections Division",
    "statute": "ORS 192.311 - 192.478",
    "z_score": 0.98,
    "ricci_curvature": 13.88
  },
  {
    "county": "Marion County",
    "state": "OR",
    "elections_email": "elections@co.marion.or.us",
    "records_officer": "Marion County Clerk Elections Division",
    "statute": "ORS 192.311 - 192.478",
    "z_score": 0.92,
    "ricci_curvature": 13.51
  },
  {
    "county": "Linn County",
    "state": "OR",
    "elections_email": "elections@co.linn.or.us",
    "records_officer": "Linn County Clerk Elections Division",
    "statute": "ORS 192.311 - 192.478",
    "z_score": 0.88,
    "ricci_curvature": 13.15
  }
]

FOIA_TEMPLATE = """TO: {records_officer} ({county}, {state})
EMAIL: {elections_email}
DATE: {date}
SUBJECT: PUBLIC RECORDS REQUEST: Precinct-Level Undeliverable Mail Logs ({county}, {state})

Dear Public Records Officer,

Under the authority of {statute}, I hereby request disclosure and electronic transmission of all electronic records, database logs, and telemetry regarding official elections mail returned as undeliverable within {county} for all election cycles spanning January 1, 2020 through the present date.

1. REQUESTED RECORD CATEGORIES:
  - Category 1: Precinct-Level Undeliverable Mail Logs (CSV/XLSX/JSON) disaggregated by Precinct, Census Tract, or ZIP Code.
  - Category 2: Active voter records reclassified to Inactive status following undeliverable mail events.
  - Category 3: Registered voters using physical descriptions as residence addresses under state law, and undelivered ballot counts for those addresses.
  - Category 4: Unhandled exception logs or helpdesk tickets regarding online ballot marking print errors.

2. FEE WAIVER REQUEST:
  - Requested in the public interest for non-commercial voter roll audit research (Z-Score = {z_score:.2f}, Ricci Curvature = {ricci_curvature:.2f}).

Sincerely,

Albert Lane & Robot
Lead Forensic Investigators
albertlane.net | aros.albertlane.net | sec.albertlane.net/estate/sec-manifest
"""

def generate_all(out_dir='/workspace/scratch/dispatched_foia_requests'):
    os.makedirs(out_dir, exist_ok=True)
    today = datetime.now().strftime('%Y-%m-%d')
    for c in COUNTIES:
        text = FOIA_TEMPLATE.format(
            records_officer=c['records_officer'],
            county=c['county'],
            state=c['state'],
            elections_email=c['elections_email'],
            statute=c['statute'],
            z_score=c['z_score'],
            ricci_curvature=c['ricci_curvature'],
            date=today
        )
        fname = f"{c['state']}_{c['county'].replace(' ', '_')}_FOIA_Request.txt"
        with open(os.path.join(out_dir, fname), 'w') as f:
            f.write(text)
    print(f'Generated requests for {len(COUNTIES)} counties in {out_dir}.')

if __name__ == '__main__':
    generate_all()
