<?php

namespace Modules\Bms\Http\Controllers;

use Illuminate\Http\Request;
use Modules\Bms\Entities\BmsArea;
use Yajra\DataTables\Facades\DataTables;

class AreaController extends BmsBaseController
{
    public function index()
    {
        $this->guard('bms.venues.view');

        // Coverage areas are auto-derived from locations that have a venue + hall.
        BmsArea::syncFromLocations($this->businessId());

        if (request()->ajax()) {
            $locations = $this->locations();

            $areas = BmsArea::where('business_id', $this->businessId())
                ->select(['id', 'name', 'name_ar', 'location_id', 'is_active']);

            return DataTables::of($areas)
                ->editColumn('location_id', fn ($a) => $locations[$a->location_id] ?? '—')
                ->editColumn('is_active', fn ($a) => $a->is_active
                    ? '<span class="label label-success">'.__('bms::lang.active').'</span>'
                    : '<span class="label label-default">'.__('bms::lang.inactive').'</span>')
                ->addColumn('action', function ($a) {
                    // Areas are auto-derived from locations; only the Arabic name is editable.
                    return '<button data-href="'.route('bms.areas.edit', $a->id).'" class="tw-dw-btn tw-dw-btn-xs tw-dw-btn-outline tw-dw-btn-primary bms-modal-btn"><i class="glyphicon glyphicon-edit"></i> '.__('messages.edit').'</button>';
                })
                ->rawColumns(['action', 'is_active'])
                ->make(true);
        }

        return view('bms::areas.index');
    }

    public function create()
    {
        $this->guard('bms.venues.manage');
        $locations = $this->locations();
        return view('bms::areas.create', compact('locations'));
    }

    public function store(Request $request)
    {
        $this->guard('bms.venues.manage');
        $data = $this->validate($request, [
            'name'        => 'required|string|max:191',
            'name_ar'     => 'nullable|string|max:191',
            'location_id' => 'nullable|integer',
            'description' => 'nullable|string',
            'is_active'   => 'nullable',
        ]);

        try {
            $data['business_id'] = $this->businessId();
            $data['is_active'] = $request->boolean('is_active');
            BmsArea::create($data);
            return $this->ok(__('bms::lang.added_success'));
        } catch (\Exception $e) {
            \Log::emergency($e->getMessage());
            return $this->fail();
        }
    }

    public function edit($id)
    {
        $this->guard('bms.venues.manage');
        $area = BmsArea::where('business_id', $this->businessId())->findOrFail($id);
        $locations = $this->locations();
        return view('bms::areas.edit', compact('area', 'locations'));
    }

    public function update(Request $request, $id)
    {
        $this->guard('bms.venues.manage');
        $data = $this->validate($request, [
            'name'        => 'required|string|max:191',
            'name_ar'     => 'nullable|string|max:191',
            'location_id' => 'nullable|integer',
            'description' => 'nullable|string',
            'is_active'   => 'nullable',
        ]);

        try {
            $area = BmsArea::where('business_id', $this->businessId())->findOrFail($id);
            $data['is_active'] = $request->boolean('is_active');
            $area->update($data);
            return $this->ok(__('bms::lang.updated_success'));
        } catch (\Exception $e) {
            \Log::emergency($e->getMessage());
            return $this->fail();
        }
    }

    public function destroy($id)
    {
        $this->guard('bms.venues.manage');
        try {
            BmsArea::where('business_id', $this->businessId())->findOrFail($id)->delete();
            return $this->ok(__('bms::lang.deleted_success'));
        } catch (\Exception $e) {
            \Log::emergency($e->getMessage());
            return $this->fail();
        }
    }
}
