<?php

namespace App\Http\Controllers\api\v1;

use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\BaseController;

class StatusControllerREST extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $query = DB::table('statuses')
            ->select('statuses.*')
            ->orderBy('statuses.name', 'asc');

        if (request()->has('private')) {
            $query->where('private', request()->get('private'));
        }

        if (request()->has('for') && request()->get('for') != 'false') {
            $query->where('for', request()->get('for'));
        }

        $query = $query->paginate(request()->get('limit', 20));

        $response = [
            'success'      => true,
            'data'         => $query->getCollection()->toArray(),
            'total'        => $query->total(),
            'current_page' => $query->currentPage(),
            'last_page'    => $query->lastPage(),
        ];

        return Response::json($response);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        $data = request()->all();
        
        $statusId = DB::table('statuses')->insertGetId([
            'name' => $data['name'],
            'description' => $data['description'] ?? '',
            'color' => $data['color'] ?? '#000000',
            'private' => $data['private'] ?? 0,
            'for' => $data['for'] ?? 'task',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        return Response::json(['success' => true, 'status_id' => $statusId]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $status = DB::table('statuses')->where('id', $id)->first();

        if (!$status) {
            return Response::json(['error' => 'Status not found'], 404);
        }

        return Response::json($status);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        $status = DB::table('statuses')->where('id', $id)->first();

        if (!$status) {
            return Response::json(['error' => 'Status not found'], 404);
        }

        $data = request()->all();
        $data['updated_at'] = now();

        DB::table('statuses')->where('id', $id)->update($data);

        return Response::json(['success' => true, 'message' => 'Status updated']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $status = DB::table('statuses')->where('id', $id)->first();

        if (!$status) {
            return Response::json(['error' => 'Status not found'], 404);
        }

        DB::table('statuses')->where('id', $id)->delete();

        return Response::json(['success' => true, 'message' => 'Status deleted']);
    }
}
