<?php

namespace HelpGent\App\Repositories;

defined( "ABSPATH" ) || exit;

use HelpGent\App\Models\Review;
use HelpGent\App\DTO\ReviewDTO;

class ReviewRepository {
    public function create( ReviewDTO $dto ) {
        $values               = $dto->to_array();
        $values['created_at'] = helpgent_now();
        return Review::query()->insert_get_id( $values );
    }

    public function update( ReviewDTO $dto ) {
        $values               = $dto->to_array();
        $values['updated_at'] = helpgent_now();
        
        return Review::query()->where( 'id', $dto->get_id() )->update( $values );
    }

    public function create_or_update( ReviewDTO $dto ) {
        $old = Review::query()->where( 'answer_id', $dto->get_answer_id() )->first();

        if ( $old ) {
            $dto->set_id( $old->id );
            return $this->update( $dto );
        }

        return $this->create( $dto );
    }

    public function delete( int $id ) {
        Review::query()->where( 'id', $id )->delete();
    }
}