using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Dapper;
using Microsoft.EntityFrameworkCore;
using MyPortal.Database.Exceptions;
using MyPortal.Database.Helpers;
using MyPortal.Database.Interfaces.Repositories;
using MyPortal.Database.Models.Connection;
using MyPortal.Database.Models.Entity;
using MyPortal.Database.Repositories.Base;
using SqlKata;
using Task = System.Threading.Tasks.Task;

namespace MyPortal.Database.Repositories;

internal class AddressAgencyRepository : BaseReadWriteRepository<AddressAgency>, IAddressAgencyRepository
{
    public AddressAgencyRepository(DbUserWithContext dbUser) : base(dbUser)
    {
    }

    protected override Query JoinRelated(Query query)
    {
        query.LeftJoin("Addresses as A", "A.Id", $"{TblAlias}.AddressId");
        query.LeftJoin("Agencies as AG", "AG.Id", $"{TblAlias}.AgencyId");

        return query;
    }

    protected override Query SelectAllRelated(Query query)
    {
        query.SelectAllColumns(typeof(Address), "A");
        query.SelectAllColumns(typeof(Agency), "AG");

        return query;
    }

    protected override async Task<IEnumerable<AddressAgency>> ExecuteQuery(Query query)
    {
        var sql = Compiler.Compile(query);

        var addressPeople =
            await DbUser.Transaction.Connection.QueryAsync<AddressAgency, Address, Agency, AddressAgency>(
                sql.Sql,
                (addressAgency, address, agency) =>
                {
                    addressAgency.Address = address;
                    addressAgency.Agency = agency;

                    return addressAgency;
                }, sql.NamedBindings, DbUser.Transaction);

        return addressPeople;
    }

    public async Task Update(AddressAgency entity)
    {
        var addressPerson = await DbUser.Context.AddressAgencies.FirstOrDefaultAsync(x => x.Id == entity.Id);

        if (addressPerson == null)
        {
            throw new EntityNotFoundException("Address person not found.");
        }

        addressPerson.AddressId = entity.AddressId;
        addressPerson.AddressTypeId = entity.AddressTypeId;
        addressPerson.Main = entity.Main;
    }

    public async Task<IEnumerable<AddressAgency>> GetByAgency(Guid agencyId)
    {
        var query = GetDefaultQuery();

        query.Where("AG.Id", agencyId);

        return await ExecuteQuery(query);
    }

    public async Task<IEnumerable<AddressAgency>> GetByAddress(Guid addressId)
    {
        var query = GetDefaultQuery();

        query.Where("A.Id", addressId);

        return await ExecuteQuery(query);
    }
}