<?php

namespace App\Services\Bot;

use App\Models\User;
use Illuminate\Support\Facades\Cache;

class ConversationStateStore
{
    private const TTL_SECONDS = 86400;

    public function get(User $user): ?string
    {
        return Cache::get($this->key($user));
    }

    public function set(User $user, ?string $state): void
    {
        if ($state === null) {
            Cache::forget($this->key($user));
            return;
        }

        Cache::put($this->key($user), $state, self::TTL_SECONDS);
    }

    private function key(User $user): string
    {
        return 'conversation_state:user:'.$user->id;
    }
}
