using BoredCareers.Entities;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.Common;
using System.Text;

namespace BoredCareers.Services.DatabaseService {
    public partial class DatabaseService {

        public async Task<Employee?> GetEmployee( int EmployeeID ) {
            Employee? employee = null;
            using( MySqlConnection connection = GetConnection() ) {
                await connection.OpenAsync();
                string command = @"
                SELECT *
                FROM Employee
                LEFT JOIN Company ON Employee.CompanyID = Company.ID
                WHERE Employee.ID = @ID;
                ";

                MySqlCommand cmd = new MySqlCommand(command, connection);
                cmd.Parameters.AddWithValue("@ID", EmployeeID);
                
                using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
                    while( await reader.ReadAsync() ) {
                        int _id = reader.GetInt32("ID");
                        int _accountid = reader.GetInt32("AccountID");
                        string _accountname = reader.GetString("AccountName");
                        string _accountemail = reader.GetString("AccountEmail");
                        int _companyid = reader.GetInt32("CompanyID");
                        string _name = reader.GetString("Name");
                        string _email = reader.GetString("Email");
                        bool _emailVerified = reader.GetBoolean("EmailVerified");
                        string _emailtoken = reader.GetString("EmailToken");
                        string _websiteurl = reader.GetString("WebsiteURL");
                        string _logo = Encoding.UTF8.GetString((byte[])reader["Logo"]);
                        string _phone = reader.GetString( "Phone" );
                        string _postalcode = reader.GetString( "PostalCode" );
                        string _country = reader.GetString( "Country" );
                        string _state = reader.GetString( "StateOrRegion" );
                        string _city = reader.GetString( "City" );
                        string _description = reader.GetString( "Description" );

                        employee = new Employee() {
                            ID = _id,
                            AccountID = _accountid,
                            AccountName = _accountname,
                            AccountEmail = _accountemail,
                            Company = new Company {
                                ID = _companyid,
                                Name = _name,
                                Email = _email,
                                EmailVerified = _emailVerified,
                                EmailToken = _emailtoken,
                                WebsiteURL = _websiteurl,
                                Logo = _logo,
                                Phone = _phone,
                                PostalCode = _postalcode,
                                Country = _country,
                                StateOrRegion = _state,
                                City = _city,
                                Description = _description
                            }
                        };
                    }
                }
            }
            return employee;
        }

        public async Task<Employee[]> GetEmployeesFromCompany(int CompanyID) {
            List<Employee> employees = new List<Employee>();
            using (MySqlConnection connection = GetConnection()) {
                await connection.OpenAsync();
                string command = @"
                SELECT *
                FROM Employee
                LEFT JOIN Company ON Employee.CompanyID = Company.ID
                WHERE Employee.CompanyID = @CompanyID;
                ";

                MySqlCommand cmd = new MySqlCommand(command, connection);
                cmd.Parameters.AddWithValue("@CompanyID", CompanyID);

                using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
                    while (await reader.ReadAsync()) {
                        int _id = reader.GetInt32("ID");
                        int _accountid = reader.GetInt32("AccountID");
                        string _accountname = reader.GetString("AccountName");
                        string _accountemail = reader.GetString("AccountEmail");
                        int _companyid = reader.GetInt32("CompanyID");
                        string _name = reader.GetString("Name");
                        string _email = reader.GetString("Email");
                        bool _emailVerified = reader.GetBoolean("EmailVerified");
                        string _emailtoken = reader.GetString("EmailToken");
                        string _websiteurl = reader.GetString("WebsiteURL");
                        string _logo = Encoding.UTF8.GetString((byte[])reader["Logo"]);
                        string _phone = reader.GetString( "Phone" );
                        string _postalcode = reader.GetString( "PostalCode" );
                        string _country = reader.GetString( "Country" );
                        string _state = reader.GetString( "StateOrRegion" );
                        string _city = reader.GetString( "City" );
                        string _description = reader.GetString( "Description" );

                        employees.Add(new Employee() {
                            ID = _id,
                            AccountID = _accountid,
                            AccountName = _accountname,
                            AccountEmail = _accountemail,
                            Company = new Company {
                                ID = _companyid,
                                Name = _name,
                                Email = _email,
                                EmailVerified = _emailVerified,
                                EmailToken = _emailtoken,
                                WebsiteURL = _websiteurl,
                                Logo = _logo,
                                Phone = _phone,
                                PostalCode = _postalcode,
                                Country = _country,
                                StateOrRegion = _state,
                                City = _city,
                                Description = _description
                            }
                        });
                    }
                }
            }
            return employees.ToArray();
        }
        
        public async Task<Employee[]> GetEmployeeOfCompanyByAccountID(int AccountID) {
            List<Employee> employees = new List<Employee>();
            using (MySqlConnection connection = GetConnection()) {
                await connection.OpenAsync();
                string command = @"
                SELECT *
                FROM Employee
                LEFT JOIN Company ON Employee.CompanyID = Company.ID
                WHERE Employee.AccountID = @AccountID;
                ";

                MySqlCommand cmd = new MySqlCommand(command, connection);
                cmd.Parameters.AddWithValue("@AccountID", AccountID);

                using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
                    while (await reader.ReadAsync()) {
                        int _id = reader.GetInt32("ID");
                        int _accountid = reader.GetInt32("AccountID");
                        string _accountname = reader.GetString("AccountName");
                        string _accountemail = reader.GetString("AccountEmail");
                        int _companyid = reader.GetInt32("CompanyID");
                        string _name = reader.GetString("Name");
                        string _email = reader.GetString("Email");
                        bool _emailVerified = reader.GetBoolean("EmailVerified");
                        string _emailtoken = reader.GetString("EmailToken");
                        string _websiteurl = reader.GetString("WebsiteURL");
                        string _logo = Encoding.UTF8.GetString((byte[])reader["Logo"]);
                        string _phone = reader.GetString( "Phone" );
                        string _postalcode = reader.GetString( "PostalCode" );
                        string _country = reader.GetString( "Country" );
                        string _state = reader.GetString( "StateOrRegion" );
                        string _city = reader.GetString( "City" );
                        string _description = reader.GetString( "Description" );

                        employees.Add(new Employee() {
                            ID = _id,
                            AccountID = _accountid,
                            AccountName = _accountname,
                            AccountEmail = _accountemail,
                            Company = new Company {
                                ID = _companyid,
                                Name = _name,
                                Email = _email,
                                EmailVerified = _emailVerified,
                                EmailToken = _emailtoken,
                                WebsiteURL = _websiteurl,
                                Logo = _logo,
                                Phone = _phone,
                                PostalCode = _postalcode,
                                Country = _country,
                                StateOrRegion = _state,
                                City = _city,
                                Description = _description
                            }
                        });
                    }
                }
            }
            return employees.ToArray();
        }

        public async Task SetEmployee(Employee employee) {
            using (MySqlConnection connection = GetConnection()) {
                await connection.OpenAsync();

                string command = @"
                INSERT INTO Employee
                (ID,AccountID,AccountName,AccountEmail,CompanyID)
                VALUES
                (@ID,@AccountID,@AccountName,@AccountEmail,@CompanyID)
                ON DUPLICATE KEY UPDATE
                AccountID = @AccountID,
                CompanyID = @CompanyID;
                ";

                MySqlCommand cmd = new MySqlCommand(command, connection);
                cmd.Parameters.AddWithValue("@ID", employee.ID);
                cmd.Parameters.AddWithValue("@AccountID", employee.AccountID);
                cmd.Parameters.AddWithValue("@AccountName", employee.AccountName);
                cmd.Parameters.AddWithValue("@AccountEmail", employee.AccountEmail);
                cmd.Parameters.AddWithValue("@CompanyID", employee.Company.ID);

                await cmd.ExecuteNonQueryAsync();
            }
        }

        public async Task DeleteEmployee( int EmployeeID ) {
            using( MySqlConnection connection = GetConnection() ) {
                MySqlCommand cmd;
                await connection.OpenAsync();

                string command = @"
                DELETE FROM Employee WHERE ID = @ID;
                ";
                cmd = new MySqlCommand( command, connection );
                cmd.Parameters.AddWithValue("@ID", EmployeeID);

                await cmd.ExecuteNonQueryAsync();
            }
        }

    }
}
