<?php

namespace App\Http\Controllers;

use App\Models\Company;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Validation\Rule;

class CompanyController extends Controller
{
    // ─── LIST ─────────────────────────────────────────────────
    public function index(): JsonResponse
    {
        $companies = Company::orderBy('legal_name')->get();

        return response()->json([
            'success' => true,
            'data'    => $companies,
        ]);
    }

    // ─── SHOW ─────────────────────────────────────────────────
    public function show(Company $company): JsonResponse
    {
        return response()->json([
            'success' => true,
            'data'    => $company,
        ]);
    }

    // ─── CREATE ───────────────────────────────────────────────
    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'tax_id'               => ['required', 'digits:11', 'unique:company,tax_id'],
            'legal_name'           => ['required', 'string', 'max:200'],
            'trade_name'           => ['nullable', 'string', 'max:200'],
            'status'               => ['nullable', 'string', Rule::in(['ACTIVE', 'INACTIVE', 'SUSPENDED'])],
            'department'           => ['nullable', 'string', 'max:50'],
            'province'             => ['nullable', 'string', 'max:50'],
            'district'             => ['nullable', 'string', 'max:50'],
            'address'              => ['nullable', 'string', 'max:300'],
        ]);

        $company = Company::create($validated);

        return response()->json([
            'success' => true,
            'message' => 'Company created successfully.',
            'data'    => $company,
        ], 201);
    }

    // ─── UPDATE ───────────────────────────────────────────────
     public function update(Request $request, $id): JsonResponse
    {
     $company = Company::findOrFail($id);

    $validated = $request->validate([
        'tax_id'      => ['sometimes', 'digits:11', Rule::unique('company', 'tax_id')->ignore($company->id)],
        'legal_name'  => ['sometimes', 'string', 'max:200'],
        'trade_name'  => ['nullable', 'string', 'max:200'],
        'status'      => ['nullable', 'string', Rule::in(['ACTIVE', 'INACTIVE', 'SUSPENDED'])],
        'department'  => ['nullable', 'string', 'max:50'],
        'province'    => ['nullable', 'string', 'max:50'],
        'district'    => ['nullable', 'string', 'max:50'],
        'address'     => ['nullable', 'string', 'max:300'],
    ]);

    $company->update($validated);

    return response()->json([
        'success' => true,
        'message' => 'Company updated successfully.',
        'data'    => $company->fresh(),
    ]);
   }

    // ─── DELETE ───────────────────────────────────────────────
    public function destroy(Company $company): JsonResponse
    {
        $company->delete();

        return response()->json([
            'success' => true,
            'message' => 'Company deleted successfully.',
        ]);
    }
}