<?php

namespace App\Http\Resources;

use App\LeadExports\LeadExportFieldType;
use App\Models\LeadExport;
use App\Models\LeadExportTemplate;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class LeadExportResource extends JsonResource
{
    public static $wrap = '';

    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    #[\Override]
    public function toArray(Request $request): array
    {
        $data = parent::toArray($request);

        // Never expose the stored secret; surface only whether one is set.
        unset($data['password']);
        $data['has_password'] = !empty($this->resource->password);

        $data['provider_key'] = $this->resource->providerKey();
        $data['docs_url'] = $this->resource->getDocsUrl();
        $data['data_options'] = [
            'modes' => LeadExport::modeOptions(),
            'field_types' => collect(LeadExportFieldType::TYPES)
                ->map(fn ($type) => ['id' => $type, 'name' => $type])
                ->values(),
            // Published system templates of the same type, offered to apply to the export.
            // Full records so the admin UI can copy mode/field_map/config client-side
            // (tenant admins cannot reach the superadmin-only template endpoint).
            'templates' => LeadExportTemplate::query()
                ->published()
                ->where('type', $this->resource->type)
                ->get(),
        ];

        return $data;
    }
}
