<?php

namespace App\Http\Controllers\Settings;

use App\Http\Controllers\Controller;
use App\Models\Branch;
use App\Models\Warehouse;
use App\Services\AuditLogService;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class BranchController extends Controller
{
    public function index()
    {
        $branches = Branch::with('defaultWarehouse')->latest()->get();
        $warehouses = Warehouse::where('status', 'active')->get();
        return view('settings.branches.index', compact('branches', 'warehouses'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'code' => 'required|string|max:50|unique:branches',
            'name_en' => 'required|string|max:255',
            'name_ar' => 'nullable|string|max:255',
            'vat_number_override' => 'nullable|string|max:50',
            'cr_number' => 'nullable|string|max:50',
            'phone' => 'nullable|string|max:50',
            'email' => 'nullable|email|max:255',
            'building_number' => 'nullable|string|max:50',
            'street_name' => 'nullable|string|max:255',
            'district' => 'nullable|string|max:255',
            'city' => 'nullable|string|max:255',
            'postal_code' => 'nullable|string|max:20',
            'additional_number' => 'nullable|string|max:20',
            'manager_name' => 'nullable|string|max:255',
            'invoice_prefix' => 'required|string|max:20',
            'default_warehouse_id' => 'nullable|exists:warehouses,id',
            'status' => 'required|in:active,inactive',
        ]);

        $branch = Branch::create($validated);

        AuditLogService::log('BRANCH_CREATED', 'Branches', $branch->id, null, $branch->toArray());

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

    public function update(Request $request, Branch $branch)
    {
        $validated = $request->validate([
            'code' => ['required', 'string', 'max:50', Rule::unique('branches')->ignore($branch->id)],
            'name_en' => 'required|string|max:255',
            'name_ar' => 'nullable|string|max:255',
            'vat_number_override' => 'nullable|string|max:50',
            'cr_number' => 'nullable|string|max:50',
            'phone' => 'nullable|string|max:50',
            'email' => 'nullable|email|max:255',
            'building_number' => 'nullable|string|max:50',
            'street_name' => 'nullable|string|max:255',
            'district' => 'nullable|string|max:255',
            'city' => 'nullable|string|max:255',
            'postal_code' => 'nullable|string|max:20',
            'additional_number' => 'nullable|string|max:20',
            'manager_name' => 'nullable|string|max:255',
            'invoice_prefix' => 'required|string|max:20',
            'default_warehouse_id' => 'nullable|exists:warehouses,id',
            'status' => 'required|in:active,inactive',
        ]);

        $oldData = $branch->toArray();
        $branch->update($validated);

        AuditLogService::log('BRANCH_UPDATED', 'Branches', $branch->id, $oldData, $branch->fresh()->toArray());

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

    public function destroy(Branch $branch)
    {
        AuditLogService::log('BRANCH_DELETED', 'Branches', $branch->id, $branch->toArray());
        $branch->delete();

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