<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\TickerItem;
use Illuminate\Http\Request;

class TickerController extends Controller
{
    public function index()
    {
        $items = TickerItem::orderBy('orden')->get();
        return view('admin.ticker.index', compact('items'));
    }

    public function create()
    {
        return view('admin.ticker.create');
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'texto' => 'required|string|max:500',
            'url'   => 'nullable|string|max:255',
            'icono' => 'nullable|string|max:100',
            'orden' => 'nullable|integer',
        ]);
        $data['activo'] = $request->has('activo') ? 1 : 0;
        TickerItem::create($data);
        return redirect()->route('admin.ticker.index')->with('success', 'Ítem del ticker creado correctamente.');
    }

    public function edit(TickerItem $ticker)
    {
        return view('admin.ticker.edit', compact('ticker'));
    }

    public function update(Request $request, TickerItem $ticker)
    {
        $data = $request->validate([
            'texto' => 'required|string|max:500',
            'url'   => 'nullable|string|max:255',
            'icono' => 'nullable|string|max:100',
            'orden' => 'nullable|integer',
        ]);
        $data['activo'] = $request->has('activo') ? 1 : 0;
        $ticker->update($data);
        return redirect()->route('admin.ticker.index')->with('success', 'Ítem del ticker actualizado correctamente.');
    }

    public function destroy(TickerItem $ticker)
    {
        $ticker->delete();
        return redirect()->route('admin.ticker.index')->with('success', 'Ítem del ticker eliminado correctamente.');
    }
}
