using System;
using System.Windows.Forms;
using RestaurantApp.DAL;
using RestaurantApp.Models;

namespace RestaurantApp
{
    public partial class ClientForm : Form
    {
        private ClientRepository repo = new ClientRepository();

        public ClientForm()
        {
            InitializeComponent();
            LoadData();

            btnSearch.Click += (s, e) => LoadData();
            btnRefresh.Click += (s, e) => LoadData();
            btnAdd.Click += BtnAdd_Click;
            btnEdit.Click += BtnEdit_Click;
            btnDelete.Click += BtnDelete_Click;
            dgvClients.DoubleClick += (s, e) => BtnEdit_Click(s, e);
        }

        private void LoadData()
        {
            try
            {
                var clients = repo.GetAll(txtSearch.Text);
                dgvClients.DataSource = null;
                dgvClients.DataSource = clients;

                if (dgvClients.Columns.Count > 0)
                {
                    dgvClients.Columns["ClientId"].HeaderText = "ID";
                    dgvClients.Columns["FirstName"].HeaderText = "Имя";
                    dgvClients.Columns["LastName"].HeaderText = "Фамилия";
                    dgvClients.Columns["Phone"].HeaderText = "Телефон";
                    dgvClients.Columns["Email"].HeaderText = "Email";
                    dgvClients.Columns["BirthDate"].HeaderText = "Дата рождения";
                    dgvClients.Columns["BonusPoints"].HeaderText = "Бонусы";
                    dgvClients.Columns["RegisteredAt"].HeaderText = "Дата регистрации";
                    dgvClients.Columns["FullName"].Visible = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Ошибка загрузки: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            var form = new ClientEditForm();
            if (form.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {
            if (dgvClients.CurrentRow == null)
            {
                MessageBox.Show("Выберите клиента", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            var client = (Client)dgvClients.CurrentRow.DataBoundItem;
            var form = new ClientEditForm(client);
            if (form.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            LoadData();
        }

        private void BtnDelete_Click(object sender, EventArgs e)
        {
            if (dgvClients.CurrentRow == null)
            {
                MessageBox.Show("Выберите клиента", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            var client = (Client)dgvClients.CurrentRow.DataBoundItem;
            if (MessageBox.Show($"Удалить клиента {client.FullName}?", "Подтверждение",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    repo.Delete(client.ClientId);
                    LoadData();
                    MessageBox.Show("Клиент удалён", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Ошибка удаления: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}