<?php

namespace App\Controllers;

use App\Models\DiningAreaModel;
use App\Models\DiningTableModel;

/**
 * Setup screens for dining areas + tables. Deliberately plain CRUD, same
 * shape as Phase 1's RoomTypeController/RoomController — the floor/
 * capacity view lives in RestaurantReservationController since it's a
 * reservations concern, not a setup concern.
 */
class DiningController extends BaseController
{
    public function areas()
    {
        return view('dining/areas', [
            'pageTitle' => 'Dining Areas',
            'areas'     => (new DiningAreaModel())->orderBy('name')->findAll(),
        ]);
    }

    public function createArea()
    {
        $model = new DiningAreaModel();
        $data  = ['name' => $this->request->getPost('name'), 'description' => $this->request->getPost('description')];

        if (! $model->insert($data)) {
            return $this->redirectWithModelErrors($model);
        }

        $this->logActivity('dining', 'Created dining area "' . $data['name'] . '".');

        return redirect()->to('dining/areas')->with('success', 'Dining area created.');
    }

    public function deleteArea(int $id)
    {
        $areas  = new DiningAreaModel();
        $tables = new DiningTableModel();
        $area   = $areas->find($id);

        if ($area === null) {
            return redirect()->to('dining/areas')->with('error', 'Dining area not found.');
        }

        if ($tables->where('dining_area_id', $id)->countAllResults() > 0) {
            return redirect()->to('dining/areas')->with('error', 'Cannot delete an area that still has tables assigned to it.');
        }

        $areas->delete($id);
        $this->logActivity('dining', 'Deleted dining area "' . $area->name . '".');

        return redirect()->to('dining/areas')->with('success', 'Dining area deleted.');
    }

    public function tables()
    {
        return view('dining/tables', [
            'pageTitle' => 'Dining Tables',
            'tables'    => (new DiningTableModel())->withArea()->orderBy('dining_areas.name')->orderBy('dining_tables.table_number')->findAll(),
            'areas'     => (new DiningAreaModel())->orderBy('name')->findAll(),
        ]);
    }

    public function createTable()
    {
        $model = new DiningTableModel();
        $data = [
            'table_number'     => $this->request->getPost('table_number'),
            'dining_area_id'   => (int) $this->request->getPost('dining_area_id'),
            'seating_capacity' => (int) $this->request->getPost('seating_capacity'),
        ];

        if (! $model->insert($data)) {
            return $this->redirectWithModelErrors($model);
        }

        $this->logActivity('dining', 'Created table ' . $data['table_number'] . '.');

        return redirect()->to('dining/tables')->with('success', 'Table created.');
    }

    public function deleteTable(int $id)
    {
        $model = new DiningTableModel();
        $table = $model->find($id);

        if ($table === null) {
            return redirect()->to('dining/tables')->with('error', 'Table not found.');
        }

        $model->delete($id);
        $this->logActivity('dining', 'Deleted table ' . $table->table_number . '.');

        return redirect()->to('dining/tables')->with('success', 'Table deleted.');
    }
}
