<?php

namespace App\Exports;

use App\Models\CustomField;
use App\Models\Enums\CustomFieldType;
use App\Models\NamingRule;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;

class NameRuleExport implements FromCollection, WithHeadings
{
    public function collection()
    {
        $objects = collect([]);
        $customFields=CustomField::query()->where('group',request()->group)->get();
        foreach ($customFields as $customField){
            $key=$customField['key'];
            $options=$customField->options;
            $inputs=$customField->inputs;
            $type=CustomFieldType::from($customField->type)->name;
            if($customField->type==3){
                foreach ($options as $option){
                    $customKey=$option['lang']['en'];
                    $customValue=$option['value'];
                    $objects->push($this->formatRow($key,$type,$customKey,$customValue));
                }
            }

            if($customField->type==1){
                foreach ($inputs as $input){
                    $customKey=$input['lang']['en'];
                    $customValue=$input['value'];
                    $objects->push($this->formatRow($key,$type,$customKey,$customValue));
                }
            }

            $objects->push($this->formatRow('','','',''));

        }

        return $objects;
        // TODO: Implement collection() method.
    }

    protected function formatRow($key,$type,$customKey,$customValue)
    {
        return [
            $key,
            $type,
            $customKey,
            $customValue,
        ];
    }

    public function headings(): array
    {
        return [
            'key',
            'type',
            'customKey',
            'customValue',
        ];
    }
}