<?php

namespace App\Http\Controllers\api\v1;

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

class KeychainControllerREST extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $query = DB::table('keychains')
            ->select('keychains.*', 'users.first_name', 'users.last_name')
            ->leftJoin('users', 'users.id', '=', 'keychains.user_id')
            ->where('keychains.user_id', Auth::user()->id)
            ->orderBy('keychains.created_at', 'desc');

        $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 = [
            'user_id' => Auth::user()->id,
            'type'        => request()->get('type'),
            'username'    => request()->get('username'),
            'password'    => request()->get('password'),
            'description' => request()->get('description', ''),
            'created_at' => now(),
            'updated_at' => now(),
        ];

        $keychainId = DB::table('keychains')->insertGetId($data);

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

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $keychain = DB::table('keychains')
            ->select('keychains.*', 'users.first_name', 'users.last_name')
            ->leftJoin('users', 'users.id', '=', 'keychains.user_id')
            ->where('keychains.id', $id)
            ->where('keychains.user_id', Auth::user()->id)
            ->first();

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

        return Response::json($keychain);
    }

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

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

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

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

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

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

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

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

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