<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Currency;
use App\Models\WithdrawOption;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class CurrencyController extends Controller
{
    public function load()
    {
        $Currency = Currency::get();
        return response()->json($Currency);
    }
    public function loadOption()
    {
        $WithdrawOption = WithdrawOption::get();
        return response()->json($WithdrawOption);
    }
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'code' => 'required|string|max:75',
        ]);
    
        $currencyName = urlencode(strtolower($request->name));
        $currencyCode = strtoupper($request->code);
        $fiatCurrencies = [
            "usd", "aed", "ars", "aud", "bdt", "bhd", "bmd", "brl", "cad", "chf",
            "clp", "cny", "czk", "dkk", "eur", "gbp", "hkd", "huf", "idr", "ils",
            "inr", "jpy", "krw", "kwd", "lkr", "mmk", "mxn", "myr", "ngn", "nok",
            "nzd", "php", "pkr", "pln", "rub", "sar", "sek", "sgd", "thb", "try",
            "twd", "uah", "vef", "vnd", "zar", "xdr", "xag", "xau"
        ];
        $fiatSymbol = implode(',', $fiatCurrencies);
    
        $response = Http::get("https://api.coingecko.com/api/v3/simple/price?ids=$currencyName&vs_currencies=$fiatSymbol");
        $data = $response->json();
    
        if (!isset($data[$currencyName])) {
            return response()->json([
                'error' => 'Currency not supported by CoinGecko.'
            ], 422);
        }
    
        $updateRow = ['currency_name' => $currencyName, 'currency_code' => $currencyCode];
        foreach ($fiatCurrencies as $fiat) {
            $updateRow[$fiat] = $data[$currencyName][$fiat] ?? null;
        }
    
        Currency::create($updateRow);
    
        return response()->json(['message' => 'Currency added successfully!']);
    }

    public function delete($id)
    {
        Currency::findOrFail($id)->delete();

        return response()->json(['message' => 'Currency deleted successfully!']);
    }
    
    public function create()
    {
        $currencies = Currency::all();
        return view('admin.withdraw.create', compact('currencies'));
    }

    public function storeOption(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:75',
            'wallet' => 'required|string|max:75',
            'api_key' => 'nullable|string|max:225',
            'currency_id' => 'required|exists:currencies,id',
            'instant' => 'required|string|max:75',
            'minimum_withdraw' => 'required|numeric|min:0',
            'fee_type' => 'required|string|in:percentage,fixed',
            'fee' => 'required|numeric|min:0',
        ]);

        $currency = Currency::findOrFail($request->currency_id);

        WithdrawOption::create([
            'name' => $request->name,
            'wallet' => $request->wallet,
            'api_key' => $request->api_key,
            'currency_id' => $currency->id,
            'currency_name' => $currency->currency_name,
            'currency_code' => $currency->currency_code,
            'instant' => $request->instant,
            'minimum_withdraw' => $request->minimum_withdraw,
            'fee_type' => $request->fee_type,
            'fee' => $request->fee
        ]);

        return response()->json(['success' => true, 'message' => 'Withdraw option added successfully!']);
    }
    public function updateOption(Request $request, $id)
    {
        $request->validate([
            'method_name' => 'required|string|max:75',
            'wallet' => 'required|string|max:75',
            'api_key' => 'nullable|string|max:225',
            'currency_id' => 'required|exists:currencies,id',
            'instant' => 'required|string|max:75',
            'minimum_withdraw' => 'required|numeric|min:0',
            'fee_type' => 'required|string|in:percentage,fixed',
            'fee' => 'required|numeric|min:0',
        ]);
    
        $withdrawOption = WithdrawOption::findOrFail($id);
        $currency = Currency::findOrFail($request->currency_id);
    
        $withdrawOption->update([
            'name' => $request->method_name,
            'wallet' => $request->wallet,
            'api_key' => $request->api_key,
            'currency_id' => $currency->id,
            'currency_name' => $currency->currency_name,
            'currency_code' => $currency->currency_code,
            'instant' => $request->instant,
            'minimum_withdraw' => $request->minimum_withdraw,
            'fee_type' => $request->fee_type,
            'fee' => $request->fee
        ]);
    
        return response()->json(['success' => true, 'message' => 'Withdraw option updated successfully!']);
    }

    public function deleteOption($id)
    {
        WithdrawOption::findOrFail($id)->delete();

        return response()->json(['message' => 'Option deleted successfully!']);
    }
}