<?php

// =====================
// Export PZ Facts - CSV
// =====================

class Export_PZFactsToCSV extends Base {

    // Variables

    private $current_user_serial = 0;
    private $current_user_id = "";
    private $current_user_name = "";
    private $current_user_email = "";

    // ==================
    // Class Constructor.
    // ==================

    public function __construct() {

        set_error_handler(array($this, "displayError"));

        date_default_timezone_set(self::TIMEZONE);

        $this->current_user_serial = $_SESSION[self::USER_SERIAL] ?? 0;
        $this->current_user_id = $_SESSION[self::USER_ID] ?? "";
        $this->current_user_name = $_SESSION[self::USER_NAME] ?? "";
        $this->current_user_email = $_SESSION[self::USER_EMAIL] ?? "";
    }

    // ===============
    // Export the Data
    // ===============

    public function export() {

        $subscription_serial = $_SESSION['subscription_serial'];
        $usersubscription_serial = $_SESSION['usersubscription_serial'];

        $subscription = (new Subscriptions())->getSubscription($subscription_serial);

        if ($subscription->count() === 0) {
            $this->logError("Invalid Subscription ({$subscription_serial}): " . __METHOD__);
        }

        $userssubscription = (new Users())->getUserSubscription($usersubscription_serial);

        $user_subscription_start_date = $userssubscription->record->usersubscription_start_date;
        $user_subscription_end_date = $userssubscription->record->usersubscription_end_date;

        $planningzoningfacts = (new PlanningAndZoningFacts())->getPZFactsPage($user_subscription_start_date, $user_subscription_end_date);

        $filename = "{$subscription->record->subscription_full_title}.csv";

        header('Content-Type: text/csv');
        header("Content-Disposition: attachment; filename=\"{$filename}\"");

        $output = fopen('php://output', 'w');

        // Add CSV headers

        $header_array = [
            'pzfact_legacy_id', 'pzfact_county', 'pzfact_owner_name', 'pzfact_owner_state', 'pzfact_owner_address', 'pzfact_owner_phone',
            'pzfact_owner_fax', 'pzfact_professional_name', 'pzfact_professional_state', 'pzfact_professional_address', 'pzfact_professional_phone', 'pzfact_professional_fax',
            'pzfact_project_type_verbose', 'pzfact_project_name', 'pzfact_project_description', 'pzfact_project_address', 'pzfact_project_city', 'pzfact_action_code_verbose',
            'pzfact_district', 'pzfact_land_lot', 'pzfact_block', 'pzfact_parcel_number', 'pzfact_lot', 'pzfact_action_date_verbose',
            'pzfact_acres', 'pzfact_status', 'pzfact_entry_date_verbose'
        ];

        fputcsv($output, $header_array, ',', '"', '\\');

        foreach ($planningzoningfacts->record as $pzfact) {

            $row = [];

            foreach ($header_array as $field) {

                // Cast SimpleXML nodes safely; empty nodes become ""
                $row[] = isset($pzfact->$field) ? trim((string) $pzfact->$field) : '';
            }

            fputcsv($output, $row, ',', '"', '\\');
        }

        fclose($output);

        exit();
    }
}

?>
