<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

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

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

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

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

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $companies = Company::all();
        $factories = Factory::all();
        return view('branches.create', compact('companies', 'factories'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'company_id' => 'required|exists:companies,id',
            'factory_id' => 'nullable|exists:factories,id',
            'name' => 'required|string|max:255',
            'location' => 'nullable|string',
            'type' => 'required|in:retail,distribution,office',
            '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();
        }

        $branch = Branch::create($request->all());

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

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

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

        return view('branches.show', compact('branch'));
    }

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

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Branch $branch)
    {
        $validator = Validator::make($request->all(), [
            'company_id' => 'sometimes|required|exists:companies,id',
            'factory_id' => 'nullable|exists:factories,id',
            'name' => 'sometimes|required|string|max:255',
            'location' => 'nullable|string',
            'type' => 'sometimes|required|in:retail,distribution,office',
            '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();
        }

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

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

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

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

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

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