using System.Data.SqlClient;
using Dapper.Contrib.Extensions;
using Dapper;
using static DbTableClass;
public class resultClass
{
    public class normalResult
    {
        public string ret = "no";
        public string err_code = "0000";
        public string message = "";
    }
    public class saveasResult
    {
        public string ret = "no";
        public string err_code = "0000";
        public string message = "";
        public quotationView quotationView = new quotationView();
        public projectView projectView = new projectView();
    }

    public class quotationDetailResult
    {
        public string ret = "no";
        public string err_code = "0000";
        public string message = "";
        public quotationDetail quotationDetail = new quotationDetail();
        public quotationView quotationView = new quotationView();
    }

    public class quotationViewListResult
    {
        public string ret = "no";
        public string err_code = "0000";
        public string message = "";
        public List<quotationView> quotationViews = new List<quotationView>();
    }

    public class projectViewResult
    {
        public string ret = "no";
        public string err_code = "0000";
        public string message = "";
        public List<projectView> projectViews = new List<projectView>();
    }

    public class modelQuotationResult
    {
        public string ret = "no";
        public string err_code = "0000";
        public string message = "";
        
        public List<modelQuotationDetail> modelQuotationDetails = new List<modelQuotationDetail>();
    }

    public class modelQuotationListResult
    {
        public string ret = "no";
        public string err_code = "0000";
        public string message = "";
        public List<modelQuotation> modelQuotations = new List<modelQuotation>();
    }
    public class modelProjListResult
    {
        public string ret = "no";
        public string err_code = "0000";
        public string message = "";
        public List<modelProj> modelProjs = new List<modelProj>();
    }
    public class contactPersonListResult
    {
        public string ret = "no";
        public string err_code = "0000";
        public string message = "";
        public List<contactPerson> contactPersons = new List<contactPerson>();
    }

    public class companyListResult
    {
        public string ret = "no";
        public string err_code = "0000";
        public string message = "";
        public List<company> companys = new List<company>();
    }
    public class authMainItemResult
    {
        public string ret = "no";
        public string err_code = "0000";
        public string message = "";
        public List<mainItem> mainItems = new List<mainItem>();
    }

    public class authSubItemResult
    {
        public string ret = "no";
        public string err_code = "0000";
        public string message = "";
        public List<subItem> subItems = new List<subItem>();
    }

    public class deptListResult
    {
        public string ret = "no";
        public string err_code = "0000";
        public string message = "";
        public List<depts> depts = new List<depts>();
    }

    public class groupUserListResult
    {
        public string ret = "no";
        public string err_code = "0000";
        public string message = "";
        public List<user> users = new List<user>();
    }

    public class groupListResult
    {
        public string ret = "no";
        public string err_code = "0000";
        public string message = "";
        public List<groupDetail> groups = new List<groupDetail>();
    }

    public class groupDetail : group
    {
   
        SqlConnection conn = new SqlConnection(GlobalClass.appsettings("ConnectionStrings:SQLConnectionString"));


        public List<user> users = new List<user>();

        public groupDetail() { }

        public groupDetail(group objData) {
            Type projectType = objData.GetType();

            foreach (var prop in projectType.GetProperties())
            {
                string propName = prop.Name;
                var valueProperty = projectType.GetProperty(propName);
                object propValue = valueProperty.GetValue(objData, null);

                this.GetType().GetProperty(propName).SetValue(this, propValue);
            }

            users = conn.Query<user>("select B.* from groupUser A, users B where A.user_uid = B.user_uid and A.group_uid = @group_uid", new {group_uid = objData.group_uid}).ToList();
        }
    }

    public class userWithDept : user 
    {
        SqlConnection conn = new SqlConnection(GlobalClass.appsettings("ConnectionStrings:SQLConnectionString"));

        public List<depts> Depts = new List<depts>();

        public userWithDept() { }

        public userWithDept(user objData) {
            Type projectType = objData.GetType();

            foreach (var prop in projectType.GetProperties())
            {
                string propName = prop.Name;
                var valueProperty = projectType.GetProperty(propName);
                object propValue = valueProperty.GetValue(objData, null);

                this.GetType().GetProperty(propName).SetValue(this, propValue);
            }

            Depts = conn.Query<depts>("select * from userDept where user_uid = @user_uid", new { user_uid = this.user_uid }).ToList();
        }
    }

    public class quotationDetail : quotation {
        SqlConnection conn = new SqlConnection(GlobalClass.appsettings("ConnectionStrings:SQLConnectionString"));

        public List<quotationMainItemDetail> quotationMainItemDetails = new List<quotationMainItemDetail>();
        public List<payment> payments = new List<payment>();
        public List<invoice> invoices = new List<invoice>();
        public user user = new user();
        public company company = new company();
        public contactPerson contactPerson = new contactPerson();

        public quotationDetail() { }

        public quotationDetail(quotation objData) {
            Type projectType = objData.GetType();

            foreach (var prop in projectType.GetProperties())
            {
                string propName = prop.Name;
                var valueProperty = projectType.GetProperty(propName);
                object propValue = valueProperty.GetValue(objData, null);

                this.GetType().GetProperty(propName).SetValue(this, propValue);
            }

            List<quotationMainItem> quotationMainItems = conn.Query<quotationMainItem>("select * from quotationMainItem where quotationMainItem_version = @quotation_version and quotation_uid = @quotation_uid ", new { quotation_version = this.quotation_version, quotation_uid = this.quotation_uid }).ToList();

            foreach (quotationMainItem qItem in quotationMainItems) {
                quotationMainItemDetails.Add(new quotationMainItemDetail(qItem));
            }
            contactPerson = conn.QueryFirstOrDefault<contactPerson>("select * from contactPerson where contactPerson_uid = @contactPerson_uid ", new { contactPerson_uid = this.contactPerson_uid });

            if (contactPerson == null) { 
                contactPerson = new contactPerson();
            }
            
            company = conn.QueryFirstOrDefault<company>("select * from company where company_uid = @company_uid", new { company_uid = this.company_uid} );

            if (company == null) { 
                company = new company();
            }
            
            user = conn.QueryFirstOrDefault<user>("select * from users where user_uid = @user_uid", new { user_uid = this.quotation_create_uid});

            if (user == null) { 
                user = new user();
            }

            payments = conn.Query<payment>("select * from payment where payment_version = @payment_version and quotation_uid = @quotation_uid ", new { payment_version = this.quotation_version, quotation_uid = this.quotation_uid }).ToList();

           
            
            invoices = conn.Query<invoice>("select * from invoice where invoice_version = @invoice_version and quotation_uid = @quotation_uid ", new { invoice_version = this.quotation_version, quotation_uid = this.quotation_uid }).ToList();


        }
    }

    public class quotationMainItemDetail : quotationMainItem {
        SqlConnection conn = new SqlConnection(GlobalClass.appsettings("ConnectionStrings:SQLConnectionString"));
        public List<quotationSubItem> quotationSubItems = new List<quotationSubItem>();

        public quotationMainItemDetail() { }

        public quotationMainItemDetail(quotationMainItem objData) {
            Type projectType = objData.GetType();

            foreach (var prop in projectType.GetProperties())
            {
                string propName = prop.Name;
                var valueProperty = projectType.GetProperty(propName);
                object propValue = valueProperty.GetValue(objData, null);

                this.GetType().GetProperty(propName).SetValue(this, propValue);
            }

            quotationSubItems = conn.Query<quotationSubItem>("select * from quotationSubItem where quotationSubItem_version = @quotationSubItem_version and quotationMainItem_uid = @quotationMainItem_uid ", new { quotationSubItem_version = this.quotationMainItem_version, quotationMainItem_uid = this.quotationMainItem_uid }).ToList(); 
        }
    }

    public class modelQuotationDetail : modelQuotation {
        SqlConnection conn = new SqlConnection(GlobalClass.appsettings("ConnectionStrings:SQLConnectionString"));
        
        public List<modelMainItemDetail> modelMainItemDetails = new List<modelMainItemDetail>();

        public modelQuotationDetail() { }

        public modelQuotationDetail(modelQuotation objData) {
            Type projectType = objData.GetType();

            foreach (var prop in projectType.GetProperties())
            {
                string propName = prop.Name;
                var valueProperty = projectType.GetProperty(propName);
                object propValue = valueProperty.GetValue(objData, null);

                this.GetType().GetProperty(propName).SetValue(this, propValue);
            }

            List<modelMainItem> modelMainItems = conn.Query<modelMainItem>("select * from modelMainItem where modelQuotation_uid = @modelQuotation_uid", new { modelQuotation_uid = objData.modelQuotation_uid }).ToList();

            foreach (modelMainItem item in modelMainItems) {
                this.modelMainItemDetails.Add(new modelMainItemDetail(item));
            }

        }
    }

    public class modelMainItemDetail: modelMainItem {
        SqlConnection conn = new SqlConnection(GlobalClass.appsettings("ConnectionStrings:SQLConnectionString"));

        public List<modelSubItem> modelSubItems = new List<modelSubItem>();

        public modelMainItemDetail() { }

        public modelMainItemDetail(modelMainItem objData) {
            Type projectType = objData.GetType();

            foreach (var prop in projectType.GetProperties())
            {
                string propName = prop.Name;
                var valueProperty = projectType.GetProperty(propName);
                object propValue = valueProperty.GetValue(objData, null);

                this.GetType().GetProperty(propName).SetValue(this, propValue);
            }

            modelSubItems = conn.Query<modelSubItem>("select * from modelSubItem where modelMainItem_uid = @modelMainItem_uid", new { modelMainItem_uid = objData.modelMainItem_uid}).ToList();
        }
    }

   
}
