<?php

namespace App\Http\Controllers;

use App\Subscription;
use App\Repositories\SubscriptionRepository;
use Illuminate\Http\Request;

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

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

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

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

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

    public function show(Request $request, $id) {
        try {
            $subs = SubscriptionRepository::find($id);
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

        auth()->user()->authorize("company_admin", $subs->company_id);

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

    public function create(Request $request) {
        auth()->user()->authorize("super_admin");

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

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

    public function update(Request $request, $id) {
        auth()->user()->authorize("super_admin");

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

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

    public function destroy($id) {
        auth()->user()->authorize("super_admin");

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

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

    public function subscribe(Request $request) {
        //1. query parameters
        $company_id = $request->query("company_id");
        $plan_id = $request->query("plan_id");
        $set_date_after_current_subscription = (bool)$request->query("set_date_after_current_subscription");
        if(!$company_id || !$plan_id) return response()->json(["error" => "Please provide company_id and plan_id as query parameters."], 400);

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

        //3. Genera plan suscripcio
        try {
            $pla = SubscriptionRepository::subscribe($company_id, $plan_id, $set_date_after_current_subscription);
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

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

    public function pay(Request $request, $id) {
        //1. query
        $override = $request->query("override") ? (bool)$request->query("override") : false;

        //2. auth
        $sub = SubscriptionRepository::find($id);
        auth()->user()->authorize("company_admin", $sub->company_id, ["active" => true, "company_active" => false]); //Molt important que estigui a fals, perque sino no podra renovar

        //3. pay
        try {
            $pla = SubscriptionRepository::pay($id, $override);
        }
        catch(\Exception $e) {
            return response()->json(["error" => $e->getMessage()], 400);
        };

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

    public function cancel($id) {
        //1. parameters
        $sub = SubscriptionRepository::find($id);
        $user_id = $sub->user_id;
        $company_id = $sub->company_id;

        //2. authorize
        if(!auth()->user()->itsMe($user_id)) {
            auth()->user()->authorize("company_admin", $company_id);
        }

        //3. Cancel
        try {
            SubscriptionRepository::cancel($id);
        }
        catch(\Exception $e) {
            return response()->json(["error" => $e->getMessage()], 400);
        };

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