<?php

namespace App\Providers;

use App\Repositories\Contracts\ReadRepositoryInterface;
use App\Repositories\Contracts\WriteRepositoryInterface;
use App\Repositories\Domain\Read\DomainReadRepository;
use App\Repositories\Domain\Write\DomainWriteRepository;
use App\Repositories\Username\Read\UsernameReadRepository;
use App\Repositories\Username\Write\UsernameWriteRepository;
use Illuminate\Support\ServiceProvider;

class RepositoryServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        // Domain Repositories
        $this->app->bind(
            ReadRepositoryInterface::class.'\\Domain',
            DomainReadRepository::class
        );

        $this->app->bind(
            WriteRepositoryInterface::class.'\\Domain',
            DomainWriteRepository::class
        );

        $this->app->singleton(DomainReadRepository::class, function ($app) {
            return new DomainReadRepository($app->make(\App\Models\Domain::class), $app->make('cache'));
        });

        $this->app->singleton(DomainWriteRepository::class, function ($app) {
            return new DomainWriteRepository($app->make(\App\Models\Domain::class), $app->make('cache'));
        });

        // Username Repositories
        $this->app->bind(
            ReadRepositoryInterface::class.'\\Username',
            UsernameReadRepository::class
        );

        $this->app->bind(
            WriteRepositoryInterface::class.'\\Username',
            UsernameWriteRepository::class
        );

        $this->app->singleton(UsernameReadRepository::class, function ($app) {
            return new UsernameReadRepository($app->make(\App\Models\Username::class), $app->make('cache'));
        });

        $this->app->singleton(UsernameWriteRepository::class, function ($app) {
            return new UsernameWriteRepository($app->make(\App\Models\Username::class), $app->make('cache'));
        });
    }

    public function provides(): array
    {
        return [
            DomainReadRepository::class,
            DomainWriteRepository::class,
            UsernameReadRepository::class,
            UsernameWriteRepository::class,
        ];
    }
}
