<?php
namespace App\Services;
use App\Enums\FeatureEnum;
use App\Exceptions\ApiHttpException;
use App\Models\Office;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Throwable;
class OfficesServices
{
    /**
     * Retrieves all offices for the authenticated account (user)
     *
     * @param User $user
     * @return Office|null
     */
    public function getOffices(User $user)
    {
        $profileId = $user->accountMember?->account?->profile?->id;
        $offices = Office::where('company_profile_id', $profileId)->get();
        return $offices;
    }

    /**
     * Creates a Office
     *
     * @param User $user
     * @param array $officeData
     * @return Office
     */
    public function createOffice(User $user, Array $officeData)
    {
        if (!$user->hasFeature(FeatureEnum::FULL_DASHBOARD, true)) {
            throw new ApiHttpException(403, trans('frontend.exception_no_permission'));
        }

        DB::beginTransaction();
        try {
            $profile = $user->accountMember?->account?->profile;
            if (!$profile) {
                throw new ApiHttpException(404, trans('frontend.exception_profile_not_found'));
            }

            $office = new Office();
            $office->companyProfile()->associate($profile);
            $office->name = $officeData['name'];
            $office->address = $officeData['address'];
            $office->postal_code = $officeData['postalCode'];
            $office->location = $officeData['location'];
            $office->floor = $officeData['floor'];
            $office->link_google_maps = $officeData['linkGoogleMaps'];
            $office->is_main_office = $officeData['isMainOffice'];
            $office->email = $officeData['email'];
            $office->phone = $officeData['phone'];
            $office->fax = $officeData['fax'];
            $office->website_url = $officeData['websiteUrl'];
            $office->country_id = $officeData['countryId'];
            $office->save();

            DB::commit();
            return $office;
        } catch (Throwable $e) {
            DB::rollback();
            throw $e;
        }
    }

    /**
     * Updates a Office
     *
     * @param User $user
     * @param array $officeData
     * @param int $officeId
     * @return Office
     */
    public function updateOffice(User $user, array $officeData, int $officeId)
    {
        if (!$user->hasFeature(FeatureEnum::FULL_DASHBOARD, true)) {
            throw new ApiHttpException(403, trans('frontend.exception_no_permission'));
        }

        $profileId = $user->accountMember?->account?->profile?->id;
        $office = Office::where('company_profile_id', $profileId)->where('id', $officeId)->first();

        if (!$office) {
            throw new ApiHttpException(404, trans('frontend.exception_office_not_found'));
        }

        DB::beginTransaction();
        try {
            $office->name = $officeData['name'];
            $office->address = $officeData['address'];
            $office->floor = $officeData['floor'];
            $office->link_google_maps = $officeData['linkGoogleMaps'];
            $office->postal_code = $officeData['postalCode'];
            $office->location = $officeData['location'];
            $office->is_main_office = $officeData['isMainOffice'];
            $office->email = $officeData['email'];
            $office->phone = $officeData['phone'];
            $office->website_url = $officeData['websiteUrl'];
            $office->country_id = $officeData['countryId'];
            if (array_key_exists('fax', $officeData)) {
                $office->fax = $officeData['fax'];
            }

            $office->save();
            DB::commit();
            $office->refresh();
            return $office;
        } catch (Throwable $e) {
            DB::rollback();
            throw $e;
        }
    }

    /**
     * Deletes a office
     *
     * @param User $user
     * @param int $officeId
     * @return Office
     */
    public function deleteOffice(User $user, int $officeId)
    {
        if (!$user->hasFeature(FeatureEnum::FULL_DASHBOARD, true)) {
            throw new ApiHttpException(403, trans('frontend.exception_no_permission'));
        }

        $profileId = $user->accountMember?->account?->profile?->id;
        $office = Office::where('company_profile_id', $profileId)->where('id', $officeId)->first();

        if (!$office) {
            throw new ApiHttpException(404, trans('frontend.exception_office_not_found'));
        }

        $office->delete();

        return $office;
    }
}
