using System.Data;
using Configuration;
using Microsoft.Data.SqlClient;
using Models;

namespace Services
{
    public interface IRollbackService
    {
        void ResetToDefaults();
    }

    public class RollbackService(SqlConnectionFactory sqlConnectionFactory) : IRollbackService
    {
        public void ResetToDefaults()
        {
            using SqlConnection conn = sqlConnectionFactory.CreateSqlConnection();

            string sql = @"
USE [master];
ALTER DATABASE [2025_5bhitn_konstantin_hintermayer_smarter_rentDB] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
RESTORE DATABASE [2025_5bhitn_konstantin_hintermayer_smarter_rentDB] FROM DATABASE_SNAPSHOT = '2025_5bhitn_konstantin_hintermayer_smarter_rentDB_snapshot';
ALTER DATABASE [2025_5bhitn_konstantin_hintermayer_smarter_rentDB] SET MULTI_USER;";

            using SqlCommand cmd = new SqlCommand(sql, conn);

            cmd.ExecuteNonQuery();
        }
    }
}