<?php

namespace App\Http\Controllers;

use App\Models\TrialAccountPaymentMethod;
use Illuminate\Http\Request;

class TrialAccountPaymentMethodController extends Controller
{
    public function index()
    {


        $companyId = auth()->user()->company_id;

        $paymentMethods = TrialAccountPaymentMethod::where('trial_company_id', $companyId)->get();


        return view('pages.trial-account.payment-list', compact('paymentMethods'));
    }

    public function addPayment()
    {

        $companyProfile = auth()->user();


        return view('pages.trial-account.payment-add', compact(
            'companyProfile'
        ));
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'payment_code' => 'required|string',
            'description' => 'required|string',
            'payment_type' => 'required|string',
            'open_drawer' => 'required|string',
            'fill_reference' => 'required|string',
            'base_color' => 'required|string',
            'text_color' => 'required|string',
        ]);


        $companyId = auth()->user()->company_id;
        $paymentCode = $request->input('payment_code');
        $description = $request->input('description');
        $paymentType = $request->input('payment_type');
        $openDrawer = $request->input('open_drawer') == 'true';
        $fillReference = $request->input('fill_reference') == 'true';
        $baseColor = $request->input('base_color');
        $textColor = $request->input('text_color');
        $icon = $request->input('icon');


        $type = 0;

        switch ($paymentType) {
            case 'CASH':
                $type = 0;
                break;
            case 'QR PAYMENT':
                $type = 1;
                break;
            case 'PAYMENT TERMINAL':
                $type = 2;
                break;
            case 'OTHERS':
                $type = 4;
                break;
        }


        $paymentMethod = TrialAccountPaymentMethod::create([
            'trial_company_id' => $companyId,
            'code' => $paymentCode,
            'image' =>  $icon,
            'description' => $description,
            'type' => $type,
            'open_drawer' => $openDrawer,
            'need_ref' => $fillReference,
            'base_color' => $baseColor,
            'text_color' => $textColor,
            'use_terminal' => false,
            'terminal_id' => '',
            'refund_pin' => '',
            'store_id' => '',
            'client_id' => '',
            'client_key' => '',
            'private_key' => '',
            'public_key' => '',
            'used_credit_card' => false,
            'used_ewallet' => false,
        ]);

        if (!$paymentMethod) {
            return response()->json([
                'message' => 'Failed to add payment method'
            ], 500);
        }

        return response()->json([
            'message' => 'Payment method added successfully',
            'payment_method' => $paymentMethod
        ]);

    }


    public function editPayment($id)
    {

        $paymentMethod = TrialAccountPaymentMethod::find($id);

        if (!$paymentMethod) {
            return redirect()->back()->with('error', 'Payment method not found');
        }

        return view('pages.trial-account.payment-edit', compact(
            'paymentMethod'
        ));
    }

    public function update(Request $request, $id)
    {

        $this->validate($request, [
            'payment_code' => 'required|string',
            'description' => 'required|string',
            'payment_type' => 'required|string',
            'open_drawer' => 'required|string',
            'fill_reference' => 'required|string',
            'base_color' => 'required|string',
            'text_color' => 'required|string',
        ]);

        $paymentMethod = TrialAccountPaymentMethod::find($id);

        if (!$paymentMethod) {
            return response()->json([
                'message' => 'Payment method not found'
            ], 404);
        }

        $paymentCode = $request->input('payment_code');
        $description = $request->input('description');
        $paymentType = $request->input('payment_type');
        $openDrawer = $request->input('open_drawer') == 'true';
        $fillReference = $request->input('fill_reference') == 'true';
        $baseColor = $request->input('base_color');
        $textColor = $request->input('text_color');
        $icon = $request->input('icon');


        $type = 0;

        switch ($paymentType) {
            case 'CASH':
                $type = 0;
                break;
            case 'QR PAYMENT':
                $type = 1;
                break;
            case 'PAYMENT TERMINAL':
                $type = 2;
                break;
            case 'OTHERS':
                $type = 4;
                break;
        }


        $paymentMethod->code = $paymentCode;
        $paymentMethod->image = $icon != '' ? $icon : $paymentMethod->image;
        $paymentMethod->description = $description;
        $paymentMethod->type = $type;
        $paymentMethod->open_drawer = $openDrawer;
        $paymentMethod->need_ref = $fillReference;
        $paymentMethod->base_color = $baseColor;
        $paymentMethod->text_color = $textColor;

        $paymentMethod->save();

        return response()->json([
            'message' => 'Payment method updated successfully',
            'payment_method' => $paymentMethod
        ]);

    }

    public function delete(Request $request)
    {

        $id = $request->input('id');

        $paymentMethod = TrialAccountPaymentMethod::find($id);

        if (!$paymentMethod) {
            return response()->json([
                'message' => 'Payment method not found'
            ], 404);
        }

        $paymentMethod->delete();

        return response()->json([
            'message' => 'Payment method deleted successfully'
        ]);

    }
}
