using GMCabsDriverAssistant.Models;
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace GMCabsDriverAssistant.Services
{
    public class SQLiteDatabaseService
    {
        private readonly SQLiteAsyncConnection _database;
        public SQLiteDatabaseService(string dbPath)
        {
            _database = new SQLiteAsyncConnection(dbPath);
            _database.CreateTableAsync<AvailableBooking>();
        }

        public Task<int> SaveBookingIdsAsync(IEnumerable<string> bookingsId)
        {
            List<AvailableBooking> availableBookings = new List<AvailableBooking>();
            AvailableBooking booking = new AvailableBooking();
            foreach (var availableBookingId in bookingsId)
            {
                try
                {
                    booking.BookingID = availableBookingId;
                    Console.WriteLine($" Booking Id : {booking.BookingID}");                  
                    availableBookings.Add(booking);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($" Exception Message : {ex.Message}");
                }

            }
            return _database.InsertAllAsync(availableBookings);
        }

        public Task<List<AvailableBooking>> GetBookingIdsAsync()
        {
            return _database.Table<AvailableBooking>().ToListAsync();
        }

        public Task<int> DeleteBookingIdsAsync(AvailableBooking bookingsId)
        {
            return _database.DeleteAsync(bookingsId); 
        }
    }
}
