<?php

namespace HelpGent\App\Repositories;

defined( 'ABSPATH' ) || exit;

use HelpGent\App\DTO\MessageForwardDTO;
use HelpGent\App\Models\MessageForward;
use HelpGent\App\Models\HelpGentUser;
use HelpGent\App\Models\Response;
use HelpGent\WpMVC\Database\Query\Builder;
use HelpGent\App\Models\Message;
use stdClass;

class ForwardRepository {
    public function create( MessageForwardDTO $message_forward_dto ) {
        $created_at = $message_forward_dto->get_created_at();

        return MessageForward::query()->insert_get_id(
            [
                'message'       => $message_forward_dto->get_message(),
                'attachment_id' => $message_forward_dto->get_attachment_id(),
                'created_at'    => $created_at ? $created_at : helpgent_now(),
            ]
        );
    }

    public function update( MessageForwardDTO $message_forward_dto ) {
        return MessageForward::query()->where( 'id', $message_forward_dto->get_id() )->update(
            [
                'message'       => $message_forward_dto->get_message(),
                'attachment_id' => $message_forward_dto->get_attachment_id(),
                'updated_at'    => helpgent_now()
            ]
        );
    }

    public function users( int $per_page, int $page, string $search = '' ) {
        $current_user = helpgent_get_current_user();
        $users        = HelpGentUser::query()->select( 'id', 'user_type', 'name', 'email' )->where( 'id', '!=', $current_user->get_id() );

        if ( ! empty( $search ) ) {
            $users->where(
                function( $query ) use( $search ) {
                    $query->where_like( 'name', "%{$search}%" )->or_where_like( 'email', "%{$search}%" );
                }
            );
        }

        return array_map(
            function( $user ) {
                $user->avatar_url = get_avatar_url( $user->email );
                return $user;
            }, $users->pagination( $page, $per_page, 5 )
        );
    }

    public function responses( int $form_id = 0, int $response_id, int $per_page, int $page, string $search = '', int $created_by = 0 ) {
        $query = Response::query( 'response' )->select( 'response.*', 'user.id as user_id', 'user.user_type as user_type', 'user.name as user_name', 'user.email as user_email' )
        ->left_join( HelpGentUser::get_table_name() . ' as user', 'user.id', 'response.created_by' )
        ->where( 'response.form_id', '=', $form_id )
        ->where( 'response.id', '!=', $response_id )
        // ->where( 'response.is_completed', 1 )
        ;

        if ( ! empty( $search ) ) {
            $query->where(
                function( $query ) use( $search ) {
                    $query->where_like( 'user.name', "%{$search}%" )->or_where_like( 'user.email', "%{$search}%" );
                }
            );
        }

        if ( 0 !== $created_by ) {
            $query->where( 'response.created_by', $created_by );
        }

        $responses = $query->with(
            [
                'message' => function ( Builder $query ) {
                    $query->order_by_desc( 'helpgent_messages.id' );
                }
            ] 
        )->pagination( $page, $per_page, 5 );

        return array_map( [$this, 'prepare_responses'] , $responses );
    }

    private function prepare_responses( stdClass &$response ) {
        if ( isset( $response->user_email ) ) {
            $avatar_url                = get_avatar_url( $response->user_email );
            $response->user_avatar_url = $avatar_url;
        }

        return $response;
    }

    public function get_by_message( string $message = '', int $attachment_id = 0 ) {
        return MessageForward::query()->where( 'message', $message )->where( 'attachment_id', $attachment_id )->first();
    }

    public function clean() {
        return MessageForward::query( 'forward' )->left_join( Message::get_table_name() . ' as message', 'forward.id', 'message.forward_id' )
        ->where_null( 'message.id' )->delete();
    }

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

    public function get_by_attachment_id_first( int $attachment_id ) {
        return MessageForward::query()->where( 'attachment_id', $attachment_id )->first();
    }
}
