<?php

namespace Vendtics\App\Repositories;

defined( "ABSPATH" ) || exit;

use Vendtics\WpMVC\Database\Query\Builder;
use Vendtics\WpMVC\Exceptions\Exception;
use Vendtics\App\Models\Note;
use Vendtics\App\DTO\Note\Read;
use Vendtics\App\DTO\Note\DTO;

class NoteRepository extends Repository {
    public function get_query_builder(): Builder {
        return Note::query( "note" );
    }

    public function get( Read $dto ) {
        $query = $this->get_query_builder()->with(
            'user', function( $query ) {
                $query->select( 'ID', 'display_name', 'user_email' );
            } 
        )->where( 'contact_id', $dto->get_contact_id() )->where( 'is_favorite', $dto->get_is_favorite() );

        $this->note_search_query( $query, $dto );

        $note_query = clone $query;

        $notes = array_map(
            function( $note ) {
                if ( ! $note->user ) {
                    return $note;
                }
                $note->user->avatar_url = get_avatar_url( $note->user->user_email );
                return $note;
            }, $query->order_by_desc( 'note.id' )->pagination( $dto->get_page(), $dto->get_per_page() )
        );

        return [
            'notes' => $notes,
            'total' => $note_query->distinct()->count( 'note.id' ),
        ];
    }

    public function get_single( int $id ) {
        return $this->get_query_builder()->where( 'note.id', $id )->with(
            'user', function( $query ) {
                $query->select( 'ID', 'display_name', 'user_email' );
            } 
        )->first();
    }

    protected function note_search_query( Builder $query, Read $dto ) {
        if ( ! $dto->is_initialized( 'search' ) ) {
            return $query;
        }

        $search = $dto->get_search();

        if ( empty( $search ) ) {
            return $query;
        }

        return $query->where(
            function( $like_query ) use( $search ) {
                $columns = [
                    'note.title',
                    'note.note'
                ];

                foreach ( $columns as $column ) {
                    $like_query->or_where_like( $column, "%{$search}%" );
                }
            }
        );
    }

    public function create( \Vendtics\WpMVC\DTO\DTO $dto ) {
        /**
         * @var DTO $dto
         */
        if ( $dto->get_contact_id() ) {

            $contact_repository = vendtics_singleton( ContactRepository::class );
            $contact            = $contact_repository->get_by_id( $dto->get_contact_id() );

            if ( ! $contact ) {
                throw new Exception( esc_html__( "Contact not found", 'vendtics' ) );
            }
        }

        return parent::create( $dto );
    }

    public function update_favorite( int $id, int $is_favorite ) {
        return $this->get_query_builder()->where( 'id', $id )->update(
            [
                'is_favorite' => $is_favorite
            ]
        );
    }
    
    public function update_priority( int $id, string $priority ) {
        return $this->get_query_builder()->where( 'id', $id )->update(
            [
                'priority' => $priority
            ]
        );
    }
}