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

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

        public async Task<Application[]> GetApplcationsFromAccount(int AccountID) {
            List<Application> applications = new List<Application>();
            using (MySqlConnection connection = GetConnection()) {
                await connection.OpenAsync();
                string command = @"
                SELECT *
                FROM JobApplication
                WHERE 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");
                        int _resumeid = reader.GetInt32("ResumeID");
                        int _joblistingid = reader.GetInt32("JobListingID");
                        string _responseemail = reader.GetString("ResponseEmail");
                        DateTime _dateapplied = reader.GetDateTime("DateApplied");
                        string _responsestatus = reader.GetString("ResponseStatus");
                        bool _hasbeenviewed = reader.GetBoolean("HasBeenViewed");
                        int _rating = reader.GetInt32("Rating");
                        string _notes = reader.GetString("Notes");

                        applications.Add(new Application() {
                            ID = _id,
                            AccountID = _accountid,
                            ResumeID = _resumeid,
                            JobListingID = _joblistingid,
                            ResponseEmail = _responseemail,
                            DateApplied = _dateapplied,
                            ResponseStatus = _responsestatus,
                            HasBeenViewed = _hasbeenviewed,
                            Rating = _rating,
                            Notes = _notes
                        });
                    }
                }
            }
            return applications.ToArray();
        }

        public async Task<Application[]> GetApplicationsFromJobListing(int JobListingID) {
            List<Application> applications = new List<Application>();
            using (MySqlConnection connection = GetConnection()) {
                await connection.OpenAsync();
                string command = @"
                SELECT *
                FROM JobApplication
                WHERE JobListingID = @JobListingID
                ";

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

                using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
                    while (await reader.ReadAsync()) {
                        int _id = reader.GetInt32("ID");
                        int _accountid = reader.GetInt32("AccountID");
                        int _resumeid = reader.GetInt32("ResumeID");
                        int _joblistingid = reader.GetInt32("JobListingID");
                        string _responseemail = reader.GetString("ResponseEmail");
                        DateTime _dateapplied = reader.GetDateTime("DateApplied");
                        string _responsestatus = reader.GetString("ResponseStatus");
                        bool _hasbeenviewed = reader.GetBoolean("HasBeenViewed");
                        int _rating = reader.GetInt32("Rating");
                        string _notes = reader.GetString("Notes");

                        applications.Add(new Application() {
                            ID = _id,
                            AccountID = _accountid,
                            ResumeID = _resumeid,
                            JobListingID = _joblistingid,
                            ResponseEmail = _responseemail,
                            DateApplied = _dateapplied,
                            ResponseStatus = _responsestatus,
                            HasBeenViewed = _hasbeenviewed,
                            Rating = _rating,
                            Notes = _notes
                        });
                    }
                }
            }
            return applications.ToArray();
        }

        public async Task<string[]> GetApplicationResponseEmailFromJobListing(int JobListingID) {
            List<string> emailadds = new List<string>();
            using (MySqlConnection connection = GetConnection()) {
                await connection.OpenAsync();
                string command = @"
                SELECT ResponseEmail
                FROM JobApplication
                WHERE JobListingID = @JobListingID
                ";

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

                using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
                    while (await reader.ReadAsync()) {
                        string _responseemail = reader.GetString("ResponseEmail");
                        emailadds.Add(_responseemail);
                    }
                }
            }
            return emailadds.ToArray();
        }

        public async Task<Application?> GetApplication(int ApplicationID) {
            Application? application = null;
            using (MySqlConnection connection = GetConnection()) {
                await connection.OpenAsync();
                string command = @"
                SELECT *
                FROM JobApplication
                WHERE ID = @ApplicationID
                ";

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

                using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
                    while (await reader.ReadAsync()) {
                        int _id = reader.GetInt32("ID");
                        int _accountid = reader.GetInt32("AccountID");
                        int _resumeid = reader.GetInt32("ResumeID");
                        int _joblistingid = reader.GetInt32("JobListingID");
                        string _responseemail = reader.GetString("ResponseEmail");
                        DateTime _dateapplied = reader.GetDateTime("DateApplied");
                        string _responsestatus = reader.GetString("ResponseStatus");
                        bool _hasbeenviewed = reader.GetBoolean("HasBeenViewed");
                        int _rating = reader.GetInt32("Rating");
                        string _notes = reader.GetString("Notes");

                        application = new Application() {
                            ID = _id,
                            AccountID = _accountid,
                            ResumeID = _resumeid,
                            JobListingID = _joblistingid,
                            ResponseEmail = _responseemail,
                            DateApplied = _dateapplied,
                            ResponseStatus = _responsestatus,
                            HasBeenViewed = _hasbeenviewed,
                            Rating = _rating,
                            Notes = _notes
                        };
                    }
                }
            }



            return application;
        }

        public async Task SetApplication(Application application) {
            using (MySqlConnection connection = GetConnection()) {
                await connection.OpenAsync();
                string command = @"
                INSERT INTO JobApplication
                (ID,AccountID,ResumeID,JobListingID,ResponseEmail,DateApplied,ResponseStatus,HasBeenViewed,Rating,Notes)
                VALUES
                (@ID,@AccountID,@ResumeID,@JobListingID,@ResponseEmail,@DateApplied,@ResponseStatus,@HasBeenViewed,@Rating,@Notes)
                ON DUPLICATE KEY UPDATE
                AccountID = @AccountID,
                ResumeID = @ResumeID,
                JobListingID = @JobListingID,
                ResponseEmail = @ResponseEmail,
                ResponseStatus = @ResponseStatus,
                HasBeenViewed = @HasBeenViewed,
                Rating = @Rating,
                Notes = @Notes;
                ";

                MySqlCommand cmd = new MySqlCommand(command, connection);
                cmd.Parameters.AddWithValue("@ID", application.ID);
                cmd.Parameters.AddWithValue("@AccountID", application.AccountID);
                cmd.Parameters.AddWithValue("@ResumeID", application.ResumeID);
                cmd.Parameters.AddWithValue("@JobListingID", application.JobListingID);
                cmd.Parameters.AddWithValue("@ResponseEmail", application.ResponseEmail);
                cmd.Parameters.AddWithValue("@DateApplied", DateTime.UtcNow);
                cmd.Parameters.AddWithValue("@ResponseStatus", application.ResponseStatus);
                cmd.Parameters.AddWithValue("@HasBeenViewed", application.HasBeenViewed);
                cmd.Parameters.AddWithValue("@Rating", application.Rating);
                cmd.Parameters.AddWithValue("@Notes", application.Notes);

                await cmd.ExecuteNonQueryAsync();
            }
        }

        public async Task DeleteApplication(int ApplicationID) {
            using (MySqlConnection connection = GetConnection()) {
                MySqlCommand cmd;
                await connection.OpenAsync();
                string command = @"
                DELETE FROM JobApplication WHERE ID = @ID;
                ";
                cmd = new MySqlCommand(command, connection);
                cmd.Parameters.AddWithValue("@ID", ApplicationID);
                await cmd.ExecuteNonQueryAsync();
            }
        }

    }
}
