import 'package:sqflite/sqflite.dart';

import '../models/staff_model.dart';
import 'database_helper.dart';

class StaffRepository {
  StaffRepository();

  final DatabaseHelper _dbHelper = DatabaseHelper();

  Future<List<StaffMember>> fetchStaff({bool includeInactive = true}) async {
    final db = await _dbHelper.database;
    final rows = await db.query(
      'staff_members',
      where: includeInactive ? null : 'is_active = 1',
      orderBy: 'name COLLATE NOCASE ASC',
    );
    return rows.map(StaffMember.fromMap).toList();
  }

  Future<void> saveStaff(StaffMember staff) async {
    final db = await _dbHelper.database;
    await db.insert(
      'staff_members',
      staff.toMap(),
      conflictAlgorithm: ConflictAlgorithm.replace,
    );
  }

  Future<void> deleteStaff(String staffId) async {
    final db = await _dbHelper.database;
    await db.delete('staff_members', where: 'id = ?', whereArgs: [staffId]);
  }
}
