<?php

namespace App\Http\Controllers\Admin\Settings;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Settings\KycFormRequest;
use App\Models\KycForm;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class KycFormController extends Controller
{
    public function index()
    {
        $buttons = [
            [
                'label' => __('Add New Field'),
                'icon' => 'ph ph-plus-bold',
                'type' => 'modal',
                'id' => 'kyc_form_add_modal_id',
                'href' => route('admin.settings.kyc-forms.store'),
            ],
        ];

        $forms = KycForm::when(request('search'), function ($query, $search) {
            return $query->where('label', 'like', '%'.$search.'%');
        })
            ->when(request('start_date') && request('end_date'), function ($query) {
                return $query->whereBetween('created_at', [
                    Carbon::parse(request('start_date'))->startOfDay(),
                    Carbon::parse(request('end_date'))->endOfDay(),
                ]);
            })
            ->orderBy('position')->get();

        $types = [
            [
                'label' => 'Text',
                'value' => 'text',
            ],
            [
                'label' => 'Number',
                'value' => 'number',
            ],
            [
                'label' => 'Date',
                'value' => 'date',
            ],
            [
                'label' => 'File',
                'value' => 'file',
            ],
            [
                'label' => 'Image',
                'value' => 'image',
            ],
            [
                'label' => 'Select',
                'value' => 'select',
            ],
        ];

        $required_types = [['label' => __('Optional'), 'value' => 0], ['label' => __('Required'), 'value' => 1]];

        return view('admin.pages.settings.kyc-form.index', compact('forms', 'buttons', 'types', 'required_types'));
    }

    public function store(KycFormRequest $request)
    {
        KycForm::create($request->validated());

        return response()->json(['message' => __('Kyc form added successfully'), 'reload' => true]);
    }

    public function update(KycFormRequest $request, KycForm $kycForm)
    {
        $kycForm->update($request->validated());

        return response()->json(['message' => __('Kyc form updated successfully'), 'reload' => true]);
    }

    public function destroy(KycForm $kycForm)
    {
        $kycForm->delete();

        return response()->json(['message' => __('Kyc form deleted successfully')]);
    }

    public function updatePosition(Request $request)
    {
        $request->validate([
            'forms' => 'required|array',
            'forms.*.id' => 'required|exists:kyc_forms,id',
            'forms.*.position' => 'required|numeric',
        ]);

        DB::transaction(function () use ($request) {
            foreach ($request->forms as $form) {
                $kycForm = KycForm::find($form['id']);
                $kycForm->position = $form['position'];
                $kycForm->save();
            }
        });

        return response()->json(['message' => __('Kyc form position updated successfully')]);
    }
}
