<?php
declare(strict_types=1);

namespace FreshCloud\Mail\ImapRequests;

final class ImapRequestBuilder implements ImapRequestBuilderInterface
{
    private int $tag = 1;
    private ?string $commandName = null;
    private array $args = [];

    public function tag(int $tag): self
    {
        $this->tag = $tag;
        return $this;
    }

    public function command(string $name, array $args = []): self
    {
        $this->commandName = $name;
        $this->args = $args;
        return $this;
    }

    public function fetch(): self
    {
        return $this->command('FETCH');
    }

    public function search(): self
    {
        return $this->command('SEARCH');
    }

    public function list_(): self
    {
        return $this->command('LIST');
    }

    public function select(): self
    {
        return $this->command('SELECT');
    }

    public function store(): self
    {
        return $this->command('STORE');
    }

    public function append(): self
    {
        return $this->command('APPEND');
    }

    public function build(): ImapCommand
    {
        return new ImapCommand(
            name: $this->commandName ?? 'NOOP',
            args: $this->args
        );
    }
}