﻿using System;
using System.Data.SQLite;
using System.Windows;
using System.Windows.Controls;

namespace EcoRideDB {
    public partial class AddMaintenance : UserControl {
        private readonly string connectionStr = "Data Source=EcoRideDB.db;Version=3;";

        public AddMaintenance() {
            InitializeComponent();
            LoadBikes();
            LoadStaff();
            txtServiceDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }

        private void LoadBikes() {
            using var connection = new SQLiteConnection(connectionStr);
            connection.Open();
            using var cmd = new SQLiteCommand("SELECT BikeID, Type FROM Bikes", connection);
            using var reader = cmd.ExecuteReader();
            while (reader.Read()) {
                cmbBike.Items.Add(new ComboBoxItem { Content = reader["Type"], Tag = reader["BikeID"] });
            }
        }

        private void LoadStaff() {
            using var connection = new SQLiteConnection(connectionStr);
            connection.Open();
            using var cmd = new SQLiteCommand("SELECT StaffID, Name FROM Staff", connection);
            using var reader = cmd.ExecuteReader();
            while (reader.Read()) {
                cmbStaff.Items.Add(new ComboBoxItem { Content = reader["Name"], Tag = reader["StaffID"] });
            }
        }

        private void AddMaintenance_Click(object sender, RoutedEventArgs e) {
            if (cmbBike.SelectedItem == null || cmbStaff.SelectedItem == null) {
                MessageBox.Show("Please select a Bike and a Staff member.", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            int bikeId = Convert.ToInt32(((ComboBoxItem)cmbBike.SelectedItem).Tag);
            int staffId = Convert.ToInt32(((ComboBoxItem)cmbStaff.SelectedItem).Tag);
            string serviceDate = txtServiceDate.Text.Trim();
            string notes = txtNotes.Text.Trim();

            if (string.IsNullOrWhiteSpace(serviceDate)) serviceDate = DateTime.Now.ToString("yyyy-MM-dd");

            if (SaveMaintenance(bikeId, staffId, serviceDate, notes)) {
                MessageBox.Show("Maintenance record added successfully!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
                txtNotes.Clear();
            } else {
                MessageBox.Show("Failed to add maintenance.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private bool SaveMaintenance(int bikeId, int staffId, string serviceDate, string notes) {
            try {
                using var connection = new SQLiteConnection(connectionStr);
                connection.Open();
                string query = "INSERT INTO Maintenance (BikeID, StaffID, ServiceDate, Notes) VALUES (@BikeID, @StaffID, @ServiceDate, @Notes)";
                using var cmd = new SQLiteCommand(query, connection);
                cmd.Parameters.AddWithValue("@BikeID", bikeId);
                cmd.Parameters.AddWithValue("@StaffID", staffId);
                cmd.Parameters.AddWithValue("@ServiceDate", serviceDate);
                cmd.Parameters.AddWithValue("@Notes", string.IsNullOrWhiteSpace(notes) ? DBNull.Value : (object)notes);
                cmd.ExecuteNonQuery();
                return true;
            } catch (Exception ex) {
                MessageBox.Show($"Error saving maintenance: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
        }
    }
}