import 'package:sefer_driver/constant/table_names.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';

class DbSql {
  static final DbSql instance = DbSql._();

  static Database? _database;

  DbSql._();

  Future<Database> get database async {
    if (_database != null) return _database!;
    _database = await _initDatabase();
    return _database!;
  }

  Future<Database> _initDatabase() async {
    String path = join(await getDatabasesPath(), 'my_database.db');
    return await openDatabase(
      path,
      version: 3,
      onCreate: (db, version) async => await _createTables(db),
      onUpgrade: (db, oldVersion, newVersion) async => await _createTables(db),
    );
  }

  Future<void> _createTables(Database db) async {
    await db.execute('''
          CREATE TABLE IF NOT EXISTS  ${TableName.carLocations}(
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            driver_id TEXT,
            latitude REAL,
            longitude REAL,
            created_at TEXT,
            updated_at TEXT
          )
        ''');
    await db.execute('''
          CREATE TABLE IF NOT EXISTS ${TableName.placesFavorite}(
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            latitude REAL,
            longitude REAL,
            name TEXT UNIQUE,
            rate TEXT
          )
        ''');
    await db.execute('''
          CREATE TABLE IF NOT EXISTS ${TableName.recentLocations}(
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            latitude REAL,
            longitude REAL,
            name TEXT ,
            rate TEXT
          )
        ''');
    await db.execute('''
          CREATE TABLE IF NOT EXISTS ${TableName.driverOrdersRefuse}(
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            order_id TEXT UNIQUE,
            created_at TEXT,
            driver_id TEXT
          )
        ''');
    await db.execute('''
          CREATE TABLE IF NOT EXISTS ${TableName.rideLocation}(
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            order_id TEXT ,
            created_at TEXT,
            lat TEXT,
            lng TEXT
          )
        ''');
    await db.execute('''
          CREATE TABLE IF NOT EXISTS ${TableName.faceDetectTimes}(
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            faceDetectTimes INTEGER
          )
        ''');

    await db.execute('''
          CREATE TABLE IF NOT EXISTS ${TableName.behavior} (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            driver_id TEXT,
            latitude REAL,
            longitude REAL,
            acceleration REAL,
            created_at TEXT,
            updated_at TEXT
          );
        ''');
    await db.execute('''
          CREATE TABLE IF NOT EXISTS ${TableName.captainNotification}(
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            faceDetectTimes INTEGER
          )
        ''');
    await db.execute('''
  CREATE TABLE IF NOT EXISTS ${TableName.applyRideFromOverLay}(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    start_location_lat TEXT,
    start_location_lng TEXT,
    end_location_lat TEXT,
    end_location_lng TEXT,
    total_passenger TEXT,
    total_driver TEXT,
    duration_to_ride TEXT,
    distance TEXT,
    driver_id TEXT,
    passenger_id TEXT,
    passenger_name TEXT,
    passenger_token_fcm TEXT,
    passenger_phone TEXT,
    duration_by_passenger TEXT,
    distance_by_passenger TEXT,
    is_wallet_checked TEXT,
    driver_token TEXT,
    duration_to_passenger TEXT,
    ride_id TEXT,
    ride_timer_begin TEXT,
    driver_id_duplicate TEXT,
    duration_to_ride_duplicate TEXT,
    way_points TEXT,
    place_coordinate_0 TEXT,
    place_coordinate_1 TEXT,
    place_coordinate_2 TEXT,
    place_coordinate_3 TEXT,
    place_coordinate_4 TEXT,
    cost_for_driver TEXT,
    passenger_wallet_total TEXT,
    passenger_email TEXT,
    start_name_address TEXT,
    end_name_address TEXT,
    car_type TEXT,
    kazan TEXT,
    passenger_rate TEXT
  )
''');
  }

  Future<List<Map<String, dynamic>>> getAllData(String table) async {
    Database db = await instance.database;
    return await db.query(table);
  }

  Future<List<Map<String, dynamic>>> getCustomQuery(String query) async {
    Database db = await instance.database;
    return await db.rawQuery(query);
  }

  Future<int> insertData(Map<String, dynamic> map, String table) async {
    Database db = await instance.database;
    return await db.insert(table, map);
  }

  Future<int> updateData(Map<String, dynamic> map, String table, int id) async {
    Database db = await instance.database;

    return await db.update(table, map, where: 'id = ?', whereArgs: [id]);
  }

  Future<int> deleteData(String table, int id) async {
    Database db = await instance.database;
    return await db.delete(table, where: 'id = ?', whereArgs: [id]);
  }

  Future<int> deleteAllData(String table) async {
    Database db = await instance.database;
    return await db.delete(table);
  }
}
