<?php

namespace App\Http\Controllers;

use App\Repositories\BoxRepository;
use App\Repositories\CompanyRepository;
use App\Repositories\PricingRepository;
use Illuminate\Http\Request;
use mysql_xdevapi\Exception;

class PricingController extends Controller
{

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

    public function index(Request $request) {
        //1. Query params

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

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

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

    public function show($id) {
        //1. Params

        //2. Authorize
        //sense autoritzacio

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

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

    public function create(Request $request) {
        //1. Params
        $company_id = $request->company_id;

        //2. Authorize
        auth()->user()->authorize("company_admin", $company_id);

        //3. Create it
        try {
            $pricing = PricingRepository::create($request->all(), $request->box_ids);
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

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

    public function update(Request $request, $id) {
        //1. Params
        $company_id = PricingRepository::company($id)->id;

        //2. Authorize
        auth()->user()->authorize("company_admin", $company_id);

        //3. Retrieve
        try {
            $pricing = PricingRepository::update($request->all(), $id, $request->box_ids);
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

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

    public function destroy($id){
        //1. Params
        $company_id = PricingRepository::company($id)->id;

        //2. Authorize
        auth()->user()->authorize("company_admin", $company_id);

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

        return response()->json("OK",200);
    }

    public function userSubscriptions(Request $request, $id) {
        //1. Params
        $active = $request->query("active") == "Y";
        $box_ids = PricingRepository::box_ids($id);

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

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

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

}
