<?php

namespace App\Http\Controllers;

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

class NewsController 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 {
            $news = NewsRepository::all();
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

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

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

        //2. Params & authorize
        if($news->company_id != null) auth()->user()->authorize("company_admin", $news->company_id);
        else if($news->box_id != null) auth()->user()->authorize("box_admin", $news->box_id);
        else return response()->json(["error" => "Esta noticia no tiene ni company_id ni box_id."], 400);

        return response()->json($news, 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->box_id;
        $company_id = $request->company_id;

        //Authorize
        if($company_id != null) auth()->user()->authorize("company_admin", $company_id);
        else if($box_id != null) auth()->user()->authorize("box_admin", $box_id);
        else return response()->json(["error" => "Esta noticia no tiene ni company_id ni box_id."], 400);

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

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

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

        //2. Authorize
        if($company_id != null) auth()->user()->authorize("company_admin", $company_id);
        else if($box_id != null) auth()->user()->authorize("box_admin", $box_id);
        else return response()->json(["error" => "Esta noticia no tiene ni company_id ni box_id."], 400);

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

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

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

        //2. Authorize
        if($news->company_id != null) auth()->user()->authorize("company_admin", $news->company_id);
        else if($news->box_id != null) auth()->user()->authorize("box_admin", $news->box_id);
        else return response()->json(["error" => "Esta noticia no tiene ni company_id ni box_id."], 400);

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

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

}
