<?php

namespace App\Http\Controllers;

use App\Repositories\PreferenceRepository;
use Illuminate\Http\Request;

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

    public function index(Request $request) {
        //1. Params - No en necessita

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

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

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

    public function show($key) {
        //sense autoritzacio

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

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

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

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

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