using Ato.CD.Inbound.Shared;
using Ato.EN.IntegrationServices.CodeGenerationDISTBENTRT;
using Ato.EN.IntegrationServices.CodeGenerationSMSFAR;
using VaTS;
using DataContracts;
using static Ato.CD.Inbound.Shared.ValidatorHelpers;
using System.Collections.Generic;

namespace Ato.CD.Inbound.SMSFAR
{
    internal class CrossFormValidatorDISTBENTRT : ICrossFormValidator
    {
        private SMSFAR2026 ParentDocument { get; }

        private DISTBENTRT2024 ChildDocument { get; }

        internal CrossFormValidatorDISTBENTRT(SMSFAR2026 report, BusinessDocument childDocument)
        {
            ParentDocument = report;
            ChildDocument = (DISTBENTRT2024)childDocument.ConsumedReport;
        }

        public List<ProcessMessageDocument> ValidateCrossFormRules()
        {
            var response = new List<ProcessMessageDocument>();

            VRATOSMSFAR437195(response);
            VRATOSMSFAR437196(response);
            VRATOSMSFAR437197(response);
            VRATOSMSFAR437200(response);
            VRATOSMSFARW00005(response);


            return response;
        }



        #region VR.ATO.SMSFAR.437195
        /* VR.ATO.SMSFAR.437195
            The Tax File Number (TFN) provided for the Distributions to Beneficiaries of Trust schedule must be the same as the TFN of the parent form

            Legacy Rule Format:
            "IF COUNT(SCHEDULE = ""DISTBENTRT"") = 1 AND RP:entity.identifier.TFN <> [DISTBENTRT3]
                RETURN VALIDATION MESSAGE
            ENDIF"

            Technical Business Rule Format:
            DISTBENTRTs.Any(d => d.DISTBENTRT3 != ParentDocument.RPIdentifierTFN)
        */
        protected void VRATOSMSFAR437195(List<ProcessMessageDocument> response)
        {
            bool assertion = (ParentDocument.RPExists == true) && ( ChildDocument.DISTBENTRT3!=null) && (ChildDocument.DISTBENTRT3!= ParentDocument.RPIdentifierTFN);

            if (assertion)
            {
                response.Add(new ProcessMessageDocument
                {
                    Code = "CMN.ATO.SMSFAR.437195",
                    Severity = ProcessMessageSeverity.Error,
                    Description = @"Tax File Number for the Distributions to Beneficiaries of Trust schedule is invalid",
                    LongDescription = @"The Tax File Number (TFN) provided for the Distributions to Beneficiaries of Trust schedule must be the same as the TFN of the parent form",
                    Location = "/xbrli:xbrl/xbrli:context/xbrli:entity/xbrli:identifier",
                    Parameters = new ProcessMessageParameters
                    {
                        new ProcessMessageParameter { Name = "RuleIdentifier", Value = "VR.ATO.SMSFAR.437195" },
                        new ProcessMessageParameter { Name = "RPIdentifierTFN", Value = ParentDocument.RPIdentifierTFN },
                        new ProcessMessageParameter { Name = "DISTBENTRT3", Value = ChildDocument.DISTBENTRT3 },
                    },
                    DocumentSequence = "parent_doc"
                });
            }
        }
        #endregion

        #region VR.ATO.SMSFAR.437196
        /* VR.ATO.SMSFAR.437196
            Reporting period start date for the Distributions to Beneficiaries of Trust schedule must be the same as the Reporting period start date of the parent form

            Legacy Rule Format:
            "IF COUNT(SCHEDULE = ""DISTBENTRT"") = 1 AND RP:period.startDate <> [DISTBENTRT1]
                RETURN VALIDATION MESSAGE
            ENDIF"

            Technical Business Rule Format:
            DISTBENTRTs.Any(d => d.DISTBENTRT1 != ParentDocument.RPStartDate)
                    
        */
        protected void VRATOSMSFAR437196(List<ProcessMessageDocument> response)
        {
            bool assertion =(ParentDocument.RPExists == true) && (ChildDocument.DISTBENTRT1!=null) && (ChildDocument.DISTBENTRT1 != ParentDocument.RPStartDate);

            if (assertion)
            {
                response.Add(new ProcessMessageDocument
                {
                    Code = "CMN.ATO.SMSFAR.437196",
                    Severity = ProcessMessageSeverity.Error,
                    Description = @"Reporting period start date for the Distributions to Beneficiaries of Trust schedule is invalid",
                    LongDescription = @"Reporting period start date for the Distributions to Beneficiaries of Trust schedule must be the same as the Reporting period start date of the parent form",
                    Location = "/xbrli:xbrl/xbrli:context/xbrli:period/xbrli:startDate",
                    Parameters = new ProcessMessageParameters {
                        new ProcessMessageParameter { Name = "RuleIdentifier", Value = "VR.ATO.SMSFAR.437196" },
                        new ProcessMessageParameter { Name = "RPStartDate", Value = ParentDocument.RPStartDate.Value.ToString("yyyy-MM-dd") },
                        new ProcessMessageParameter { Name = "DISTBENTRT1", Value = ChildDocument.DISTBENTRT1.Value.ToString("yyyy-MM-dd") },
                    },
                    DocumentSequence = "parent_doc"
                });
            }
        }
        #endregion

        #region VR.ATO.SMSFAR.437197
        /* VR.ATO.SMSFAR.437197
            Reporting period end date for the Distributions to Beneficiaries of Trust schedule must be the same as the Reporting period end date of the parent form

            Legacy Rule Format:
            "IF COUNT(SCHEDULE = ""DISTBENTRT"") = 1 AND RP:period.endDate <> [DISTBENTRT2]
                RETURN VALIDATION MESSAGE
            ENDIF"

            Technical Business Rule Format:
            DISTBENTRTs.Any(d => d.DISTBENTRT2 != ParentDocument.RPEndDate)
                    
        */
        protected void VRATOSMSFAR437197(List<ProcessMessageDocument> response)
        {
            bool assertion = (ParentDocument.RPExists == true)&& (ChildDocument.DISTBENTRT2 != null)  &&  (ChildDocument.DISTBENTRT2 != ParentDocument.RPEndDate);

            if (assertion)
            {
                response.Add(new ProcessMessageDocument
                {
                    Code = "CMN.ATO.SMSFAR.437197",
                    Severity = ProcessMessageSeverity.Error,
                    Description = @"Reporting period end date for the Distributions to Beneficiaries of Trust schedule is invalid",
                    LongDescription = @"Reporting period end date for the Distributions to Beneficiaries of Trust schedule must be the same as the Reporting period end date of the parent form",
                    Location = "/xbrli:xbrl/xbrli:context/xbrli:period/xbrli:endDate",
                    Parameters = new ProcessMessageParameters
                    {
                        new ProcessMessageParameter {Name = "RuleIdentifier", Value = "VR.ATO.SMSFAR.437197"},
                        new ProcessMessageParameter { Name = "RPEndDate", Value = ParentDocument.RPEndDate.Value.ToString("yyyy-MM-dd") },
                        new ProcessMessageParameter { Name = "DISTBENTRT1", Value = ChildDocument.DISTBENTRT1.Value.ToString("yyyy-MM-dd") }
                       
                    },
                    DocumentSequence = "parent_doc"
                    

                });
            }
        }
        #endregion

        #region VR.ATO.SMSFAR.437200
        /*  VR.ATO.SMSFAR.437200
        Junior minerals exploration incentive (JMEI) law is no longer operative. For information about claiming the junior minerals exploration incentive, see ato.gov.au.

        Legacy Rule Format:
        IF COUNT(SCHEDULE = "DISTBENTRT") = 1 AND ANY OCCURRENCE OF ([DISTBENTRT27] <> NULL)
            RETURN VALIDATION MESSAGE
        ENDIF

        Technical Business Rule Format:
        ^DISBENTRT27 <> NULL

        Data Elements:

        ^DISBENTRT27 = RP:IncomeTax.TaxOffsetExplorationCredits.Amount
        */
        protected void VRATOSMSFAR437200(List<ProcessMessageDocument> response)
        {
            if (ChildDocument.RP_StatementOfDistributionToBeneficiaryCollection != null)
            {
                foreach (DISTBENTRT2024.RP_StatementOfDistributionToBeneficiary statementOfDistributionToBeneficiary in ChildDocument.RP_StatementOfDistributionToBeneficiaryCollection)
                {
                    if (statementOfDistributionToBeneficiary.DISTBENTRT27 != null)
                    {
                        response.Add(new ProcessMessageDocument
                        {
                            Code = "CMN.ATO.SMSFAR.437200",
                            Severity = ProcessMessageSeverity.Error,
                            Description = @"Exploration credit tax offset must not be provided",
                            LongDescription = @"Junior minerals exploration incentive (JMEI) law is no longer operative. For information about claiming the junior minerals exploration incentive, see ato.gov.au.",
                            Location = "/tns:DISTBENTRT/tns:RP/tns:StatementOfDistributionToBeneficiaryCollection/tns:StatementOfDistributionToBeneficiary" + OccurrenceIndex(statementOfDistributionToBeneficiary.OccurrenceIndex) + "/tns:Information/tns:IncomeTaxExplorationCreditsDistributedA",
                            Parameters = new ProcessMessageParameters
                            {
                               new ProcessMessageParameter { Name = "RuleIdentifier", Value = "VR.ATO.SMSFAR.437200" },
                               new ProcessMessageParameter { Name = "DISTBENTRT27", Value = GetValueOrEmpty(statementOfDistributionToBeneficiary.DISTBENTRT27.Value) },
                            },
                            DocumentSequence = "parent_doc"
                        });
                    }
                }
            }
        }
        #endregion

        #region VR.ATO.SMSFAR.W00005
        /* VR.ATO.SMSFAR.W00005
            Distributions to Beneficiaries of Trust schedule is provided but Gross distributions from trusts income is not declared

            Legacy Rule Format:
            "IF COUNT(SCHEDULE = ""DISTBENTRT"") = 1 AND ([SMSFAR47] = NULL)
                RETURN VALIDATION MESSAGE
            ENDIF"

            Technical Business Rule Format:
            DISTBENTRTs.Any() &&  !ParentDocument.SMSFAR47.HasValue
                    
        */
        protected void VRATOSMSFARW00005(List<ProcessMessageDocument> response)
        {
            bool assertion = !ParentDocument.SMSFAR47.HasValue;

            if (assertion)
            {
                response.Add(new ProcessMessageDocument
                {
                    Code = "CMN.ATO.SMSFAR.W00005",
                    Severity = ProcessMessageSeverity.Warning,
                    Description = @"Ensure Gross distributions from trusts income is declared",
                    LongDescription = @"Ensure Gross distributions from trusts income is declared when Distributions to Beneficiaries of Trust schedule is provided",
                    Location = "/xbrli:xbrl/Income.TrustDistributionGross.Amount",
                    Parameters = new ProcessMessageParameters
                    {
                        new ProcessMessageParameter { Name = "RuleIdentifier", Value = "VR.ATO.SMSFAR.W00005" },
                        new ProcessMessageParameter { Name = "SMSFAR47", Value = ParentDocument.SMSFAR47.HasValue ? ParentDocument.SMSFAR47.Value.ToString() : "null" }
                    },
                    DocumentSequence = "parent_doc"
                });
            }
        }
        #endregion

    }
}