<?php

/**
 * JSON format exporter.
 *
 * @package Leadite
 */
namespace Leadite\Export;

defined( 'ABSPATH' ) || exit;
class JsonExporter extends Exporter {
    public function format() {
        return 'json';
    }

    public function mime_type() {
        return 'application/json; charset=utf-8';
    }

    public function export( array $leads ) {
        // Batch-fetch all meta in one query to avoid N+1.
        $meta_map = \Leadite\Storage\LeadsRepository::find_many( $leads );
        $lead_ids = array_map( function ( $l ) {
            return (int) $l->id;
        }, $leads );
        $tags_map = array();
        $notes_map = array();
        $export_data = array();
        foreach ( $leads as $lead ) {
            $lead_fields = ( isset( $meta_map[(int) $lead->id] ) ? $meta_map[(int) $lead->id]['fields'] : array() );
            $row = (array) $lead;
            if ( !\Leadite\Pro::is_active() ) {
                unset($row['starred']);
            }
            $row['fields'] = array();
            foreach ( $lead_fields as $f ) {
                $row['fields'][] = array(
                    'key'       => $f->field_key,
                    'label'     => $f->field_label,
                    'value'     => $f->field_value,
                    'type'      => $f->field_type,
                    'canonical' => $f->canonical,
                );
            }
            // Include attachments with their status (pending/failed/skipped/ready).
            $file_rows = \Leadite\Storage\LeadsRepository::find_files( (int) $lead->id );
            $row['attachments'] = array();
            foreach ( $file_rows as $file ) {
                $mime_state = (string) $file->mime;
                if ( 'skipped' === $mime_state ) {
                    $row['attachments'][] = array(
                        'field_key'         => $file->field_key,
                        'file_name'         => $file->file_name,
                        'attachment_status' => 'skipped_by_policy',
                        'reason'            => ( isset( $file->skip_reason ) ? $file->skip_reason : '' ),
                    );
                } elseif ( 'pending' === $mime_state || 'failed' === $mime_state ) {
                    $row['attachments'][] = array(
                        'field_key'         => $file->field_key,
                        'file_name'         => $file->file_name,
                        'attachment_status' => $mime_state,
                    );
                } else {
                    $row['attachments'][] = array(
                        'field_key'         => $file->field_key,
                        'file_name'         => $file->file_name,
                        'attachment_status' => 'stored',
                        'mime'              => $file->mime,
                        'size_bytes'        => (int) $file->size_bytes,
                    );
                }
            }
            $export_data[] = $row;
        }
        // Downloaded as a application/json file (see headers above), not embedded in HTML.
        // Use default encoding (no JSON_UNESCAPED_* ) so all output stays fully escaped.
        echo wp_json_encode( $export_data, JSON_PRETTY_PRINT );
    }

}
