<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\TransportCompany;
use App\Models\BusRoute;
use App\Models\BusSchedule;

class TransportController extends Controller
{
    /**
     * List all transport companies and routes.
     */
    public function index()
    {
        $companies = TransportCompany::with('routes.schedules')->orderBy('name')->get();
        return view('admin.transport.index', compact('companies'));
    }

    /**
     * Store a new transport company.
     */
    public function storeCompany(Request $request)
    {
        $request->validate(['name' => 'required|string|max:255']);
        TransportCompany::create($request->all());
        return back()->with('success', 'Transport company added!');
    }

    /**
     * Store a new route for a company.
     */
    public function storeRoute(Request $request, TransportCompany $company)
    {
        $request->validate([
            'from_location' => 'required|string',
            'to_location' => 'required|string',
            'base_price' => 'required|numeric',
        ]);

        $company->routes()->create($request->all());
        return back()->with('success', 'Route added successfully!');
    }

    /**
     * Store a new schedule for a route.
     */
    public function storeSchedule(Request $request, BusRoute $route)
    {
        $request->validate([
            'departure_time' => 'required',
            'available_seats' => 'required|integer',
        ]);

        $route->schedules()->create($request->all());
        return back()->with('success', 'Schedule added!');
    }
}
