<?php

namespace App\Modules\Stock;

use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\App;
use App\Http\Controllers\BaseController;
use App\Modules\Stock\PaymentMethods;

class PaymentsControllerRESTv1 extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        // Original Laravel5 logic: Get payment methods from local table
        $query = PaymentMethods::select('*');
        
        if (request()->get('all')) {
            $payments = $query->get();
            
            // Always use local payments for stock purchase orders
            // Transform local payment data to match expected format (AccountID, AccountName)
            $transformedPayments = $payments->map(function($payment) {
                return [
                    'AccountID' => $payment->id,
                    'AccountName' => $payment->name . ' (' . $payment->provider . ' - ' . $payment->endNumber . ')',
                    'id' => $payment->id,
                    'name' => $payment->name,
                    'provider' => $payment->provider,
                    'bank' => $payment->bank,
                    'endNumber' => $payment->endNumber,
                ];
            });
            
            return Response::json([
                'success' => true,
                'data' => $transformedPayments,
                'total' => count($transformedPayments),
                'source' => 'local_payment_methods'
            ]);
        }

        $query = $query->paginate(request()->get('limit', 20));

        $response = [
            'success'      => true,
            'data'         => $query->getCollection()->toArray(),
            'total'        => $query->total(),
            'current_page' => $query->currentPage(),
            'last_page'    => $query->lastPage(),
        ];

        return Response::json($response);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        $paymentMethods = new PaymentMethods();
        $paymentMethods->name = request()->get('name');
        $paymentMethods->provider = request()->get('provider');
        $paymentMethods->bank = request()->get('bank');
        $paymentMethods->endNumber = request()->get('endNumber');

        if ($paymentMethods->save()) {
            return Response::json($paymentMethods->toArray());
        } else {
            return Response::json(['errors' => $paymentMethods->errors->all()], 400);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function show($id)
    {
        $paymentMethod = PaymentMethods::find($id);

        if (!$paymentMethod) {
            return Response::json(['error' => 'Payment method not found.'], 404);
        }

        return Response::json($paymentMethod->toArray());
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function update($id)
    {
        $paymentMethod = PaymentMethods::find($id);

        if (!$paymentMethod) {
            return Response::json(['error' => 'Payment method not found.'], 404);
        }

        $paymentMethod->name = request()->get('name', $paymentMethod->name);
        $paymentMethod->provider = request()->get('provider', $paymentMethod->provider);
        $paymentMethod->bank = request()->get('bank', $paymentMethod->bank);
        $paymentMethod->endNumber = request()->get('endNumber', $paymentMethod->endNumber);

        if ($paymentMethod->save()) {
            return Response::json($paymentMethod->toArray());
        } else {
            return Response::json(['errors' => $paymentMethod->errors->all()], 400);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $paymentMethod = PaymentMethods::find($id);

        if (!$paymentMethod) {
            return Response::json(['error' => 'Payment method not found.'], 404);
        }

        if ($paymentMethod->delete()) {
            return Response::json($paymentMethod->toArray());
        } else {
            return Response::json(['error' => 'Failed to delete payment method.'], 400);
        }
    }
}
