using Dapper;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace e_suite.Service.SigmaImporter.Repository;

public class PrintSpecDomain
{
    public long PrintSpecId { get; set; }
    public long ClientId { get; set; }
}

public class SigmaFtpuRepository : MySqlRepositoryBase, ISigmaFtpuRepository
{
    public SigmaFtpuRepository(IConfiguration configuration, ILogger logger) : base(configuration, logger, "SigmaFTPU")
    {
    }

    public async Task<IEnumerable<SigmaPrintSpecification>> GetActivePrintSpecifications()
    {
        try
        {
            var result = await Connection.QueryAsync<SigmaPrintSpecification>(
                "select\r\n    ps.psID as SigmaId,\r\n    printer.pID as SiteId,\r\n    printer.name as PrinterName,\r\n    printer.address as PrinterAddress,\r\n    printer.country as PrinterCountry,\r\n    p.name as Process,\r\n    ps.name as Name,\r\n    ps.trap as Trap,\r\n    ps.maxink as MaxInk,\r\n    ps.mpl as Mpl,\r\n    ps.mnl as Mnl,\r\n    ps.mmil as Mmil,\r\n    ps.mmif as Mmif,\r\n    ps.mpf as Mpf,\r\n    ps.mnf as Mnf,\r\n    ps.mindot as Mindot,\r\n    ps.maxdot as Maxdot,\r\n    ps.BWR as Bwr,\r\n    ps.minmag as MinMag,\r\n    ps.sequence_c as SequenceC,\r\n    ps.sequence_m as SequenceM,\r\n    ps.sequence_y as SequenceY,\r\n    ps.sequence_k as SequenceK,\r\n    ps.colours as Colours,\r\n    ps.gmgFK as GmgFk,\r\n    ps.supplymethod as SupplyMethod,\r\n    ps.print_process as PrintProcess,\r\n    ps.tech_spec as TechSpec,\r\n    ps.digital_supply as DigitalSupply,\r\n    ps.barcode as Barcode,\r\n    ps.substrate as Substrate,\r\n    ps.notes as Notes\r\nfrom ps_printspec as ps\r\nleft join ps_process as p\r\n    on p.pID = ps.pfk\r\n    and p.active = 1\r\nleft join ps_printer as printer\r\n    on printer.pID = p.pFK\r\n    and printer.active = 1\r\nwhere ps.active = 1;");
            return result;
        }
        finally
        {
            Close();
        }
    }

    public async Task<IEnumerable<string>> GetActivePrintProcesses()
    {
        try
        {
            var result = await Connection.QueryAsync<string>("select distinct p.name\r\nfrom ps_process as p\r\nwhere p.active = 1");
            return result;
        }
        finally
        {
            Close();
        }
    }

    public async Task<IEnumerable<PrintSpecDomain>> GetActivePrintSpecificationDomains()
    {
        try
        {
            var result = await Connection.QueryAsync<PrintSpecDomain>(
                "select\r\n    PS.psID as PrintSpecId, S.cFK as ClientId\r\nfrom ps_sites S\r\ninner join ps_printer PR on PR.pID=S.pFK\r\ninner join ps_process PC on PC.pFK=PR.pID\r\ninner join ps_printspec PS on PS.pFK=PC.pID\r\nand S.cFK not in (\r\n    select\r\n        pe.clientFK as clientid\r\n    from ps_exclusion as pe\r\n    where pe.printspecFK = PS.psID\r\n)\r\norder by\r\n        PS.psID,\r\n        S.cFK");
            return result;
        }
        finally
        {
            Close();
        }
    }
}