enum ProjectStatus { active, archived }

enum ProjectCrs { eov, wgs84 }

class Project {
  final int? id;
  final String uuid;
  final String name;
  final String client; // megrendelő
  final String description;
  final ProjectCrs crs;
  final String color; // hex, a UI-ban azonosításhoz
  final ProjectStatus status;
  final bool isDefault;
  final bool isLocalOnly;
  final DateTime? lastSyncedAt;
  final DateTime createdAt;
  final DateTime updatedAt;

  const Project({
    this.id,
    required this.uuid,
    required this.name,
    this.client = '',
    this.description = '',
    this.crs = ProjectCrs.eov,
    this.color = '#185FA5',
    this.status = ProjectStatus.active,
    this.isDefault = false,
    this.isLocalOnly = false,
    this.lastSyncedAt,
    required this.createdAt,
    required this.updatedAt,
  });

  Project copyWith(
          {String? name,
          String? client,
          String? description,
          ProjectCrs? crs,
          String? color,
          ProjectStatus? status}) =>
      Project(
        id: id,
        uuid: uuid,
        name: name ?? this.name,
        client: client ?? this.client,
        description: description ?? this.description,
        crs: crs ?? this.crs,
        color: color ?? this.color,
        status: status ?? this.status,
        isDefault: isDefault,
        isLocalOnly: isLocalOnly,
        lastSyncedAt: lastSyncedAt,
        createdAt: createdAt,
        updatedAt: DateTime.now(),
      );

  Map<String, dynamic> toMap() => {
        if (id != null) 'id': id,
        'uuid': uuid,
        'name': name,
        'client': client,
        'description': description,
        'crs': crs.name,
        'color': color,
        'status': status.name,
        'is_default': isDefault ? 1 : 0,
        'is_local_only': isLocalOnly ? 1 : 0,
        if (lastSyncedAt != null)
          'last_synced_at': lastSyncedAt!.toIso8601String(),
        'created_at': createdAt.toIso8601String(),
        'updated_at': updatedAt.toIso8601String(),
      };

  factory Project.fromMap(Map<String, dynamic> m) => Project(
        id: m['id'] as int?,
        uuid: m['uuid'] as String,
        name: m['name'] as String,
        client: m['client'] as String? ?? '',
        description: m['description'] as String? ?? '',
        crs: ProjectCrs.values.byName(m['crs'] as String? ?? 'eov'),
        color: m['color'] as String? ?? '#185FA5',
        status: ProjectStatus.values.byName(m['status'] as String? ?? 'active'),
        isDefault: _readBool(m, 'is_default', defaultValue: false),
        isLocalOnly: _readBool(m, 'is_local_only', defaultValue: false),
        lastSyncedAt: m['last_synced_at'] == null
            ? null
            : DateTime.parse(m['last_synced_at'] as String),
        createdAt: DateTime.parse(m['created_at'] as String),
        updatedAt: DateTime.parse(m['updated_at'] as String),
      );
}

bool _readBool(
  Map<String, dynamic> m,
  String key, {
  bool defaultValue = false,
}) {
  final value = m[key];

  if (value == null) return defaultValue;
  if (value is bool) return value;
  if (value is int) return value != 0;

  if (value is String) {
    final lower = value.toLowerCase();
    return lower == 'true' || lower == '1';
  }

  return defaultValue;
}
