<?php

namespace HelpGent\App\Repositories;

defined( 'ABSPATH' ) || exit;

use Exception;
use HelpGent\App\DTO\GuestDTO;
use HelpGent\App\Models\Guest;
use HelpGent\App\Models\User;

class GuestRepository {
    public function create( GuestDTO $dto ) {

        if ( $this->get_by_email( $dto->get_email() ) ) {
            throw new Exception( esc_html__( "This email address already exists.", 'helpgent' ), 500 );
        }

        $user = User::query()->where( 'user_email', $dto->get_email() )->first();

        if ( $user ) {
            throw new Exception( esc_html__( "This email address is already in the user table.", 'helpgent' ), 501 );
        }

        $values               = $dto->to_array();
        $values['created_at'] = helpgent_now();

        return Guest::query()->insert_get_id( $values );
    }

    public function get_by_id( int $id ) {
        return Guest::query()->where( 'id', $id )->first();
    }

    public function get_by_email( string $email ) {
        return Guest::query()->where( 'email', $email )->first();
    }

    public function get_by_token( string $token ) {
        return Guest::query()->where( 'token', $token )->first();
    }

    public function update_token( int $id ) {
        $token = helpgent_generate_token();
        Guest::query()->where( 'id', $id )->update(
            [
                'token'            => $token,
                'token_expires_at' => helpgent_now()->add_days( 30 )
            ]
        );
        return $token;
    }
}