<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreOfferFinanceFieldConfigRequest;
use App\Http\Requests\UpdateOfferFinanceFieldConfigRequest;
use App\Http\Resources\OfferFinanceFieldConfigList;
use App\Http\Resources\OfferFinanceFieldConfigView;
use Illuminate\Http\Response;
use Mtc\MercuryDataModels\OfferFinanceFieldConfig;

class OfferFinanceFieldConfigController extends Controller
{
    public function index(): OfferFinanceFieldConfigList
    {
        return new OfferFinanceFieldConfigList(
            OfferFinanceFieldConfig::query()
                ->orderBy('position')
                ->orderBy('id')
                ->get()
        );
    }

    public function store(StoreOfferFinanceFieldConfigRequest $request): OfferFinanceFieldConfigView
    {
        $config = OfferFinanceFieldConfig::query()->create($request->validated());

        return new OfferFinanceFieldConfigView($config);
    }

    public function show(OfferFinanceFieldConfig $financeField): OfferFinanceFieldConfigView
    {
        return new OfferFinanceFieldConfigView($financeField);
    }

    public function update(
        UpdateOfferFinanceFieldConfigRequest $request,
        OfferFinanceFieldConfig $financeField
    ): OfferFinanceFieldConfigView {
        $financeField->fill($request->validated());
        $financeField->save();

        return new OfferFinanceFieldConfigView($financeField);
    }

    public function destroy(OfferFinanceFieldConfig $financeField): Response
    {
        $financeField->delete();

        return response('ok');
    }
}
