<?php

namespace App\Http\Controllers;

use App\Payment;
use App\Repositories\BoxRepository;
use App\Repositories\CompanyRepository;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use App\Repositories\PaymentRepository;


class PaymentController extends Controller
{

    public function __construct() {
        $this->middleware('auth:api', ['except' => []]);
    }

    public function index(Request $request) {
        //1. Obtenim query parameters opcionals

        //2. Authorize
        auth()->user()->authorize("super_admin");

        try {
            $payments = PaymentRepository::all();
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

        return response()->json($payments, 200);
    }

    public function show($id) {
        //1. Render
        try {
            $payment = PaymentRepository::find($id);
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

        //2. Params
        $user_id = $payment->user->id;

        //3. Authorize
        if(!auth()->user()->itsMe($user_id)) {
            auth()->user()->authorize("box_admin", $payment->box->id);
        }

        return response()->json($payment, 200);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * S'ha d'especificar sí o sí box_id, per temes d'autorització. Payment no té referència directa amb box o company
     */
    public function create(Request $request) {
        //1. Params
        $box_id = $request->query("box_id");
        if(!$box_id) return response()->json(["message" => "Tienes que especificar box_id!"], 400);

        //Authorize
        auth()->user()->authorize("box_admin", $box_id);

        try {
            $payments = PaymentRepository::create($request->all());
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

        return response()->json($payments, 200);
    }

    public function update(Request $request, $id) {
        //1. Params
        $box_id = PaymentRepository::box($id)->id;

        //2. Authorize
        auth()->user()->authorize("box_admin", $box_id);

        try {
            $payments = PaymentRepository::update($request->all(), $id);
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

        return response()->json($payments, 200);
    }

    public function destroy($id) {
        //1. Params
        $box_id = PaymentRepository::box($id)->id;

        //2. Authorize
        auth()->user()->authorize("box_admin", $box_id);

        try {
            PaymentRepository::delete($id);
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

        return response()->json(["message" => "Ok"], 200);
    }

    public function pay(Request $request, $id) {
        return PaymentRepository::pay($id,$request['payment_type_id'],$request['start_date']);
    }

}
