<?php
declare(strict_types=1);

namespace App\Services\ShopServices;

use Exception;
use App\Models\Shop;
use App\Models\User;
use App\Traits\Notification;
use App\Services\CoreService;
use App\Helpers\ResponseError;
use App\Models\PushNotification;

class ShopActivityService extends CoreService
{
    use Notification;

    /**
     * @return string
     */
    protected function getModelClass(): string
    {
        return Shop::class;
    }

    public function changeStatus(string $uuid, array $data): array
    {
        /** @var Shop $shop */
        $shop = $this->model()
            ->with(['seller'])
            ->whereHas('seller')
            ->firstWhere('uuid', $uuid);

        if (!$shop) {
            return [
                'status'  => false,
                'message' => __('errors.' . ResponseError::ERROR_404, locale: $this->language)
            ];
        }

        $shop->update($data);

        /** @var User $user */
        $user = auth('sanctum')->user();

        if ($data['status'] == 'approved' && !$user->hasRole('admin')) {
            $shop->seller->syncRoles('seller');
        }

        $messageKey = ResponseError::SHOP_STATUS_CHANGED;

        $this->sendNotification(
            $shop,
            $shop->seller->firebase_token ?? [],
            __("errors.$messageKey", ['status' => $shop->status], $shop->seller?->lang ?? $this->language),
            __("errors.$messageKey", ['status' => $shop->status], $shop->seller?->lang ?? $this->language),
            [
                'id' => $shop->user_id,
                'type' => PushNotification::STATUS_CHANGED
            ],
            [$shop->user_id]
        );

        return ['status' => true, 'message' => ResponseError::NO_ERROR, 'data' => $shop];
    }

    /**
     * @throws Exception
     */
    public function changeOpenStatus(string $uuid): void
    {
        $shop = $this->model()->firstWhere('uuid', $uuid);

        if (empty($shop)) {
            throw new Exception( __('errors.' . ResponseError::ERROR_404, locale: $this->language));
        }

        /** @var Shop $shop */
        $shop->update(['open' => !$shop->open]);
    }

}
