<?php

namespace EasyRestaurantManager\Http\Controllers;

use EasyRestaurantManager\Http\Requests\StoreCouponRequest;
use EasyRestaurantManager\Http\Requests\StoreMenuRequest;
use EasyRestaurantManager\Http\Requests\StoreTableRequest;
use EasyRestaurantManager\Resources\CouponResource;
use EasyRestaurantManager\Resources\TableResource;
use WP_REST_Request;

class TableController
{
   

    public static function getTable(WP_REST_Request $request)
    {
        $date = sanitize_text_field( $request->get_param('date') );
        $time = sanitize_text_field( $request->get_param('time') );
        $isAllTable = sanitize_text_field( $request->get_param('is_all_table') );
        $res = [];
        if ($isAllTable == 'yes') {
            $res = TableResource::getAllTable();
        } else {
            if (!$date || !$time) {
                return rest_ensure_response([
                    'message' => 'Reservation date and time is required'
                ], 500);
            }
            $res = TableResource::getTable($date, $time);
        }
        

        if (!$res) {
            return rest_ensure_response([
                'message' => 'Failed to create table'
            ], 500);
        }
        return rest_ensure_response([
            'message' => 'table get successfully',
            'tables' => $res
        ]);
    }

    public static function postTable(WP_REST_Request $request){

        $data = $request->get_param('data');
        $id = $data['id'];

        $validatedRequest = new StoreTableRequest($request);
        if ($validatedRequest->fails()) {
            return rest_ensure_response([
                'message' => 'Validation failed',
                'errors'  => $validatedRequest->errors()
            ], 400);
        }

        if ($id) {
            $res = TableResource::updateTable($data);
            return($res);
            if ($res === false) {
                return rest_ensure_response([
                    'message' => 'Failed to update table'
                ], 500);
            }
            return rest_ensure_response([
                'message' => 'table updated successfully',
                'table_data' => $res
            ]);
        }

        $res = TableResource::store($validatedRequest->validated());
        if (!$res) {
            return rest_ensure_response([
                'message' => 'Failed to create table'
            ], 500);
        }
        return rest_ensure_response([
            'message' => 'Table created successfully',
            'table_data' => $res
        ]);

    }

    public static function deleteTable(WP_REST_Request $request){
        $table_id = $request->get_param('id');
        if (!$table_id) {
            return rest_ensure_response([
                'message' => 'Table id is required'
            ], 400);
        }
        $res = TableResource::deleteTable($table_id);
        
        if (!$res) {
            return rest_ensure_response([
                'message' => 'Failed to delete table'
            ], 500);
        }
        return rest_ensure_response([
            'message' => 'Table deleted successfully'
        ]);
    }
 
}
