<?php

namespace App\Http\Controllers\Tenant;

use App\Http\Controllers\Controller;
use App\Models\CorporateCompany;
use App\Models\CorporateContact;
use Illuminate\Http\Request;

class CorporateCompanyController extends Controller
{
    public function index()
    {
        $tenantId = auth()->user()->tenant_id;
        $companies = CorporateCompany::where('tenant_id', $tenantId)->with(['contacts', 'corporateEmployees'])->paginate(15);

        return view('tenant.corporate.companies.index', compact('companies'));
    }

    public function create()
    {
        return view('tenant.corporate.companies.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'name_en' => 'required|string|max:255',
            'trade_license_number' => 'nullable|string',
        ]);

        $tenant = auth()->user()->tenant;
        $code = 'COM-'.date('Y').'-'.str_pad(rand(1, 9999), 4, '0', STR_PAD_LEFT);

        $company = CorporateCompany::create([
            'tenant_id' => $tenant->id,
            'company_code' => $code,
            'name_en' => $request->name_en,
            'name_ar' => $request->name_ar,
            'legal_name' => $request->legal_name ?? $request->name_en,
            'business_type' => $request->business_type ?? 'LLC',
            'trade_license_number' => $request->trade_license_number,
            'trade_license_expiry' => $request->trade_license_expiry,
            'trn' => $request->trn,
            'establishment_number' => $request->establishment_number,
            'email' => $request->email,
            'phone' => $request->phone,
            'mobile' => $request->mobile,
            'emirate' => $request->emirate ?? 'Dubai',
            'status' => 'active',
            'credit_limit' => $request->credit_limit ?? 0,
            'credit_days' => $request->credit_days ?? 30,
        ]);

        return redirect()->route('app.corporate.companies.show', $company->id)->with('status', 'Corporate Client '.$company->name_en.' created successfully.');
    }

    public function show($id)
    {
        $tenantId = auth()->user()->tenant_id;
        $company = CorporateCompany::where('tenant_id', $tenantId)->with(['contacts', 'corporateEmployees.documents', 'documents'])->findOrFail($id);

        return view('tenant.corporate.companies.show', compact('company'));
    }

    public function addContact(Request $request, $id)
    {
        $request->validate(['name' => 'required|string', 'mobile' => 'required|string']);

        $tenantId = auth()->user()->tenant_id;
        $company = CorporateCompany::where('tenant_id', $tenantId)->findOrFail($id);

        CorporateContact::create([
            'tenant_id' => $tenantId,
            'company_id' => $company->id,
            'name' => $request->name,
            'designation' => $request->designation,
            'mobile' => $request->mobile,
            'email' => $request->email,
            'authorization_level' => $request->authorization_level ?? 'general',
            'is_primary' => $request->boolean('is_primary'),
        ]);

        return back()->with('status', 'Authorized corporate contact added.');
    }
}
