<?php

namespace App\Http\Controllers;

use App\Http\Requests\ExchangeRateRequest;
use App\Models\ExchangeRate;
use App\Models\Setting;
use App\Services\ExchangeRateService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Inertia\Inertia;

class ExchangeRateController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $perPage = request('per_page', 10);

        $exchangeRates = ExchangeRate::query()
            ->latest()
            ->paginate($perPage)
            ->withQueryString();


        $currency_settings =  Setting::group('currency')->pluck('value', 'key');

        return Inertia::render('dashboard/currency-settings/exchange-rates', [
            'exchange_rates' => $exchangeRates,
            'currency_settings' => $currency_settings
        ]);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(ExchangeRateRequest $request)
    {
        DB::beginTransaction();

        try {

            ExchangeRate::create([
                'base_currency' => $request->base_currency,
                'rates' => $request->rates,
                'updated_by' => $request->updated_by,
                'recorded_at' => now(),
                'last_updated_at' => $request->last_updated_at ?? now(),
            ]);

            DB::commit();

            return back();
        } catch (\Exception $e) {

            DB::rollBack();

            return back()->withErrors(['error' => $e->getMessage()]);
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(ExchangeRateRequest $request, ExchangeRate $exchangeRate)
    {
        DB::beginTransaction();

        try {

            $exchangeRate->update([
                'base_currency' => $request->base_currency,
                'rates' => $request->rates,
                'updated_by' => $request->updated_by,
                'recorded_at' => now(),
                'last_updated_at' => $request->last_updated_at ?? now(),
            ]);

            DB::commit();

            return back();
        } catch (\Exception $e) {

            DB::rollBack();

            return back()->withErrors([
                'error' => 'Something went wrong while updating exchange rates.',
            ]);
        }
    }

    public function addRecord()
    {
        $result = ExchangeRateService::addRecord();

        if (!$result) {
            return back()->with('error', 'Failed to update exchange rates');
        }

        return back()->with('success', 'Exchange rates updated successfully');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(ExchangeRate $exchangeRate)
    {
        try {
            $exchangeRate->delete();

            return back();
        } catch (\Exception $e) {

            return back()->withErrors([
                'error' => 'Something went wrong while deleting exchange rates.',
            ]);
        }
    }
}
