<?php

namespace App\Http\Controllers\Settings;

use App\Http\Controllers\Controller;
use App\Models\CompanyProfile;
use App\Services\AuditLogService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class CompanyController extends Controller
{
    public function edit()
    {
        $company = CompanyProfile::getProfile();
        return view('settings.company', compact('company'));
    }

    public function update(Request $request)
    {
        $company = CompanyProfile::getProfile();
        $oldData = $company->toArray();

        $validated = $request->validate([
            'company_name_en' => 'required|string|max:255',
            'company_name_ar' => 'nullable|string|max:255',
            'trade_name' => 'nullable|string|max:255',
            'legal_name' => 'nullable|string|max:255',
            'vat_number' => 'nullable|string|max:50',
            'cr_number' => 'nullable|string|max:50',
            'building_number' => 'nullable|string|max:50',
            'street_name_en' => 'nullable|string|max:255',
            'street_name_ar' => 'nullable|string|max:255',
            'district_en' => 'nullable|string|max:255',
            'district_ar' => 'nullable|string|max:255',
            'city_en' => 'nullable|string|max:255',
            'city_ar' => 'nullable|string|max:255',
            'postal_code' => 'nullable|string|max:20',
            'additional_number' => 'nullable|string|max:20',
            'country' => 'required|string|max:100',
            'country_code' => 'required|string|max:10',
            'phone' => 'nullable|string|max:50',
            'mobile' => 'nullable|string|max:50',
            'email' => 'nullable|email|max:255',
            'website' => 'nullable|string|max:255',
            'bank_name' => 'nullable|string|max:255',
            'bank_account_name' => 'nullable|string|max:255',
            'account_number' => 'nullable|string|max:50',
            'iban' => 'nullable|string|max:50',
            'default_currency' => 'required|string|max:10',
            'currency_display' => 'required|string|max:10',
            'vat_registered' => 'boolean',
            'default_vat_rate' => 'required|numeric|min:0|max:100',
            'tax_invoice_footer_en' => 'nullable|string',
            'tax_invoice_footer_ar' => 'nullable|string',
            'terms_conditions_en' => 'nullable|string',
            'terms_conditions_ar' => 'nullable|string',
            'received_by_label' => 'nullable|string|max:255',
            'logo' => 'nullable|image|max:2048',
            'invoice_logo' => 'nullable|image|max:2048',
            'company_stamp' => 'nullable|image|max:2048',
            'authorized_signature' => 'nullable|image|max:2048',
        ]);

        $uploadFields = [
            'logo' => 'logo_path',
            'invoice_logo' => 'invoice_logo_path',
            'company_stamp' => 'company_stamp_path',
            'authorized_signature' => 'authorized_signature_path',
        ];

        foreach ($uploadFields as $inputKey => $columnKey) {
            if ($request->hasFile($inputKey)) {
                $path = $request->file($inputKey)->store('company', 'public');
                $validated[$columnKey] = $path;
            }
        }

        $validated['vat_registered'] = $request->boolean('vat_registered');

        $company->update($validated);

        AuditLogService::log('COMPANY_UPDATED', 'Settings', $company->id, $oldData, $company->fresh()->toArray());

        return redirect()->back()->with('success', 'Company profile updated successfully.');
    }
}
