<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Factory;
use App\Models\Company;
use App\Models\User;
use Illuminate\Support\Facades\Validator;

class FactoryController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $factories = Factory::with('company', 'manager')->paginate(10);

        if ($request->wantsJson() || $request->is('api/*')) {
            return response()->json($factories);
        }

        return view('factories.index', compact('factories'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $companies = Company::all();
        $managers = User::all(); // In real app, filter by role
        return view('factories.create', compact('companies', 'managers'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'company_id' => 'required|exists:companies,id',
            'name' => 'required|string|max:255',
            'address' => 'nullable|string',
            'manager_id' => 'nullable|exists:users,id',
            'status' => 'required|in:active,inactive',
        ]);

        if ($validator->fails()) {
            if ($request->wantsJson() || $request->is('api/*')) {
                return response()->json($validator->errors(), 422);
            }
            return back()->withErrors($validator)->withInput();
        }

        $factory = Factory::create($request->all());

        if ($request->wantsJson() || $request->is('api/*')) {
            return response()->json($factory, 201);
        }

        return redirect()->route('factories.index')->with('success', 'Factory created successfully.');
    }

    /**
     * Display the specified resource.
     */
    public function show(Request $request, Factory $factory)
    {
        if ($request->wantsJson() || $request->is('api/*')) {
            return response()->json($factory->load('company', 'zones.productionLines.machines'));
        }

        return view('factories.show', compact('factory'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Factory $factory)
    {
        $companies = Company::all();
        $managers = User::all();
        return view('factories.edit', compact('factory', 'companies', 'managers'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Factory $factory)
    {
        $validator = Validator::make($request->all(), [
            'company_id' => 'sometimes|required|exists:companies,id',
            'name' => 'sometimes|required|string|max:255',
            'address' => 'nullable|string',
            'manager_id' => 'nullable|exists:users,id',
            'status' => 'sometimes|required|in:active,inactive',
        ]);

        if ($validator->fails()) {
            if ($request->wantsJson() || $request->is('api/*')) {
                return response()->json($validator->errors(), 422);
            }
            return back()->withErrors($validator)->withInput();
        }

        $factory->update($request->all());

        if ($request->wantsJson() || $request->is('api/*')) {
            return response()->json($factory);
        }

        return redirect()->route('factories.index')->with('success', 'Factory updated successfully.');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Request $request, Factory $factory)
    {
        $factory->delete();

        if ($request->wantsJson() || $request->is('api/*')) {
            return response()->json(null, 204);
        }

        return redirect()->route('factories.index')->with('success', 'Factory deleted successfully.');
    }
}
