import 'package:sqflite/sqflite.dart';

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

class DepartmentRepository {
  DepartmentRepository();

  final DatabaseHelper _dbHelper = DatabaseHelper();

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

  Future<void> saveDepartment(Department department) async {
    final db = await _dbHelper.database;
    await db.insert(
      'departments',
      department.toMap(),
      conflictAlgorithm: ConflictAlgorithm.replace,
    );
  }

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