<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\AppointmentTag;
use Illuminate\Support\Facades\Validator;

class AppointmentTagController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $tags = AppointmentTag::orderBy('order', 'asc')->get();
        return view('appointment-tags.index', compact('tags'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // No necesitamos una página separada para crear, se hace en la página index
        return redirect()->route('appointment-tags.index');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255',
            'color' => 'required|string|max:10',
        ], [
            'name.required' => __('appointment-tags.name_required'),
            'color.required' => __('appointment-tags.color_required'),
        ]);

        if ($validator->fails()) {
            return redirect()->route('appointment-tags.index')
                ->withErrors($validator)
                ->withInput();
        }

        // Establecer el orden
        $maxOrder = AppointmentTag::max('order') ?? 0;

        AppointmentTag::create([
            'name' => $request->name,
            'color' => $request->color,
            'active' => $request->has('active') ? true : false,
            'order' => $maxOrder + 1,
        ]);

        return redirect()->route('appointment-tags.index')
            ->with('success', __('appointment-tags.tag_created'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        // No necesitamos una página de detalle, redirigir al índice
        return redirect()->route('appointment-tags.index');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        // No necesitamos una página separada para editar, se hace mediante modal
        return redirect()->route('appointment-tags.index');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255',
            'color' => 'required|string|max:10',
        ], [
            'name.required' => __('appointment-tags.name_required'),
            'color.required' => __('appointment-tags.color_required'),
        ]);

        if ($validator->fails()) {
            return redirect()->route('appointment-tags.index')
                ->withErrors($validator)
                ->withInput();
        }

        $tag = AppointmentTag::findOrFail($id);
        
        $tag->name = $request->name;
        $tag->color = $request->color;
        $tag->active = $request->has('active') ? true : false;
        $tag->save();

        return redirect()->route('appointment-tags.index')
            ->with('success', __('appointment-tags.tag_updated'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $tag = AppointmentTag::findOrFail($id);
        $tag->delete();

        return redirect()->route('appointment-tags.index')
            ->with('success', __('appointment-tags.tag_deleted'));
    }
    
    /**
     * Update the order of the tags
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function updateOrder(Request $request)
    {
        $tags = $request->input('tags', []);
        
        foreach ($tags as $index => $tagId) {
            $tag = AppointmentTag::find($tagId);
            if ($tag) {
                $tag->order = $index;
                $tag->save();
            }
        }
        
        return response()->json(['success' => true]);
    }
}
