<?php

namespace App\Services;

use App\Libs\Services\FcmService;
use App\Models\FcmMessage;
use App\Services\DataService;
use Throwable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;

class FcmMessageDataService extends DataService
{


    public const FAVOURITE_ENDING_EXHIBITION_TYPE  = 'favourite_ending_exhibition';
    public const FAVOURITE_OPENING_TOMORROW_EXHIBITION_TYPE = 'favourite_opening_tomorrow_exhibition';
    public const FAVOURITE_ARTIST_EXHIBITION_OPENING_TOMORROW_TYPE = 'favourite_artist_exhibition_opening_tomorrow';
    public const FAVOURITE_ARTIST_EXHIBITION_ENDING_TOMORROW_TYPE = 'favourite_artist_exhibition_ending_tomorrow';
    public const FAVOURITE_ARTIST_EXHIBITION_NEW_EXHIBIT_TYPE = 'favourite_artist_exhibition_new_exhibit';

    /**
     * Add a record for a sent Fcm Message
     * @param string $topicName
     * @param string $fcmId
     * @param array $messageContents
     * @param mixed ?$content
     */
    public function addFcmTopicMessage(string $topicName, string $fcmId, array $messageContents, $content = null, $controlWeek = null): ?FcmMessage
    {

        $contentId   = $content ? $content->id : null;
        $contentType = $content ? get_class($content) : 'n/a';
        $mContents   = json_encode($messageContents);

        $data = [
            'type'             => 'topic',
            'type_identifier'  => $topicName,
            'content_id'       => $contentId,
            'content_type'     => $contentType,
            'fcm_id'           => $fcmId,
            'message_contents' => $mContents,
            'week_sent'        => $controlWeek,
        ];
        try {
            $fcmMessage = FcmMessage::create($data);
            return $fcmMessage;
        } catch (Throwable $t) {
            LOG::error($t->getMessage());
        }
        return null;
    }



    /**
     * Add a record for a sent Fcm Message
     * @param string $fcmId
     * @param array $messageContents
     * @param mixed ?$content
     */
    public function addTokenMessage(
        int $userId,
        string $typeIdentifier,
        string $fcmId,
        int $contentId,
        string $contentType,
        array $messageContents
    ): ?FcmMessage {


        $mContents   = json_encode($messageContents);

        $data = [
            'type'             => 'token',
            'user_id'          => $userId,
            'type_identifier'  => $typeIdentifier,
            'content_id'       => $contentId,
            'content_type'     => $contentType,
            'fcm_id'           => $fcmId,
            'message_contents' => $mContents,
        ];
        try {
            $fcmMessage = FcmMessage::create($data);
            return $fcmMessage;
        } catch (Throwable $t) {
            LOG::error($t->getMessage());
        }
        return null;
    }

    /**
     * Check if a message was sent for a week notifying for new exhibitions in a certain week
     * 
     * @param string $week (formatted weeknum_year )
     * 
     * @return bool 
     */
    public function wasWeeklyExhibitionsMessageSent(string $week): bool
    {

        $messages = DB::table('fcm_messages')
            ->where('type', 'topic')
            ->where('type_identifier', FcmService::TOPIC_WEEKLY_OPENINGS)
            ->where('week_sent', $week)
            ->count();

        return $messages === 0 ? false : true;
    }
}
