<?php

namespace App\Http\Controllers;

use App\Models\Table;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class TableController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        // Add CORS headers
        $headers = [
            'Access-Control-Allow-Origin' => '*',
            'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS',
            'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With',
        ];

        try {
            $query = Table::query();

            // Filter by section
            if ($request->has('section')) {
                $query->where('section', $request->section);
            }

            // Filter by status
            if ($request->has('status')) {
                $query->where('status', $request->status);
            }

            $tables = $query->orderBy('name')->get();

            return response()->json($tables->map(function ($table) {
                return [
                    'id' => $table->id,
                    'name' => $table->name,
                    'section' => $table->section,
                    'section_display' => $table->section_display,
                    'capacity' => $table->capacity,
                    'status' => $table->status,
                    'status_display' => $table->status_display,
                    'status_color' => $table->status_color,
                    'is_active' => $table->is_active,
                    'is_available' => $table->isAvailable(),
                ];
            }))->withHeaders($headers);
        } catch (\Exception $e) {
            return response()->json([
                'error' => 'Failed to fetch tables',
                'message' => $e->getMessage()
            ], 500)->withHeaders($headers);
        }
    }

    public function store(Request $request): JsonResponse
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'section' => 'required|string|in:dining,bar,outdoor,vip,private',
            'capacity' => 'required|integer|min:1|max:20',
        ]);

        $table = Table::create([
            'branch_id' => $request->user()->branch_id,
            'name' => $request->name,
            'section' => $request->section,
            'capacity' => $request->capacity,
            'status' => 'available',
        ]);

        return response()->json([
            'message' => 'Table created successfully',
            'table' => $table->load('branch'),
        ], 201);
    }

    public function update(Request $request, Table $table): JsonResponse
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'section' => 'required|string|in:dining,bar,outdoor,vip,private',
            'capacity' => 'required|integer|min:1|max:20',
            'is_active' => 'boolean',
        ]);

        $table->update([
            'name' => $request->name,
            'section' => $request->section,
            'capacity' => $request->capacity,
            'is_active' => $request->is_active ?? true,
        ]);

        return response()->json([
            'message' => 'Table updated successfully',
            'table' => $table->load('branch'),
        ]);
    }

    public function updateStatus(Request $request, Table $table): JsonResponse
    {
        $request->validate([
            'status' => 'required|in:available,occupied,reserved,maintenance',
        ]);

        $table->update(['status' => $request->status]);

        return response()->json([
            'message' => 'Table status updated successfully',
            'table' => $table->fresh(),
        ]);
    }

    public function options(): JsonResponse
    {
        $headers = [
            'Access-Control-Allow-Origin' => '*',
            'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS',
            'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With',
        ];

        return response()->json([])->withHeaders($headers);
    }
}