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<Company?> GetCompany( int CompanyID ) {
            Company? company = null;
            using( MySqlConnection connection = GetConnection() ) {
                await connection.OpenAsync();
                string command = @"
                SELECT *
                FROM Company
                WHERE ID = @ID;
                ";

                MySqlCommand cmd = new MySqlCommand(command, connection);
                cmd.Parameters.AddWithValue("@ID", CompanyID);
                
                using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) {
                    while( await reader.ReadAsync() ) {
                        int _id = reader.GetInt32("ID");
                        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"]);
                        int _jobsclosedsuccessful = reader.GetInt32("JobsClosedSuccessful");
                        int _jobsautoclosed = reader.GetInt32("JobsAutoClosed");
                        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" );

                        company = new Company() {
                            ID = _id,
                            Name = _name,
                            Email = _email,
                            EmailVerified = _emailVerified,
                            EmailToken = _emailtoken,
                            WebsiteURL = _websiteurl,
                            Logo = _logo,
                            JobsAutoClosed = _jobsautoclosed,
                            JobsClosedSuccessful = _jobsclosedsuccessful,
                            Phone = _phone,
                            PostalCode = _postalcode,
                            Country = _country,
                            StateOrRegion = _state,
                            City = _city,
                            Description = _description
                        };
                    }
                }
            }
            return company;
        }

        public async Task<int> SetCompany( Company company ) {
            using (MySqlConnection connection = GetConnection()) {
                await connection.OpenAsync();
                string command = @"
                INSERT INTO Company
                (ID,Name,Email,EmailVerified,EmailToken,WebsiteURL,Logo,JobsClosedSuccessful,JobsAutoClosed,Phone,PostalCode,Country,StateOrRegion,City,Description)
                VALUES
                (@ID,@Name,@Email,@EmailVerified,@EmailToken,@WebsiteURL,@Logo,@JobsClosedSuccessful,@JobsAutoClosed,@Phone,@PostalCode,@Country,@StateOrRegion,@City,@Description)
                ON DUPLICATE KEY UPDATE
                Name = @Name,
                Email = @Email,
                EmailVerified = @EmailVerified,
                EmailToken = @EmailToken,
                WebsiteURL = @WebsiteURL,
                Logo = @Logo,
                JobsClosedSuccessful = @JobsClosedSuccessful,
                JobsAutoClosed = @JobsAutoClosed,
                Phone = @Phone,
                PostalCode = @PostalCode,
                Country = @Country,
                StateOrRegion = @StateOrRegion,
                City = @City,
                Description = @Description;

                Select LAST_INSERT_ID();
                ";

                MySqlCommand cmd = new MySqlCommand(command, connection);
                cmd.Parameters.AddWithValue("@ID", company.ID);
                cmd.Parameters.AddWithValue("@Name", company.Name);
                cmd.Parameters.AddWithValue("@Email", company.Email);
                cmd.Parameters.AddWithValue("@EmailVerified", company.EmailVerified);
                cmd.Parameters.AddWithValue("@EmailToken", company.EmailToken);
                cmd.Parameters.AddWithValue("@WebsiteURL", company.WebsiteURL);
                cmd.Parameters.AddWithValue("@Logo", Encoding.UTF8.GetBytes(company.Logo));
                cmd.Parameters.AddWithValue("@JobsClosedSuccessful", company.JobsClosedSuccessful);
                cmd.Parameters.AddWithValue("@JobsAutoClosed", company.JobsAutoClosed);
                cmd.Parameters.AddWithValue("@Phone", company.Phone);
                cmd.Parameters.AddWithValue("@PostalCode", company.PostalCode);
                cmd.Parameters.AddWithValue("@Country", company.Country);
                cmd.Parameters.AddWithValue("@StateOrRegion", company.StateOrRegion);
                cmd.Parameters.AddWithValue("@City", company.City);
                cmd.Parameters.AddWithValue("@Description", company.Description);

                object? result = await cmd.ExecuteScalarAsync();
                int insertedId = result != null ? Convert.ToInt32(result) : 0;
                return insertedId;
            }
        }

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

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

                await cmd.ExecuteNonQueryAsync();
            }
        }

    }
}
