/// 결재 템플릿 엔티티
///
/// - 반복되는 결재 단계를 사전에 정의해두고 요청 시 불러온다.
class ApprovalTemplate {
  ApprovalTemplate({
    required this.id,
    required this.code,
    required this.name,
    this.description,
    this.note,
    required this.isActive,
    this.createdBy,
    this.createdAt,
    this.updatedAt,
    this.steps = const [],
  });

  final int id;
  final String code;
  final String name;
  final String? description;
  final String? note;
  final bool isActive;
  final ApprovalTemplateAuthor? createdBy;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final List<ApprovalTemplateStep> steps;

  ApprovalTemplate copyWith({
    String? code,
    String? name,
    String? description,
    String? note,
    bool? isActive,
    ApprovalTemplateAuthor? createdBy,
    DateTime? createdAt,
    DateTime? updatedAt,
    List<ApprovalTemplateStep>? steps,
  }) {
    return ApprovalTemplate(
      id: id,
      code: code ?? this.code,
      name: name ?? this.name,
      description: description ?? this.description,
      note: note ?? this.note,
      isActive: isActive ?? this.isActive,
      createdBy: createdBy ?? this.createdBy,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      steps: steps ?? this.steps,
    );
  }
}

class ApprovalTemplateAuthor {
  ApprovalTemplateAuthor({
    required this.id,
    required this.employeeNo,
    required this.name,
  });

  final int id;
  final String employeeNo;
  final String name;
}

class ApprovalTemplateStep {
  ApprovalTemplateStep({
    this.id,
    required this.stepOrder,
    required this.approver,
    this.note,
  });

  final int? id;
  final int stepOrder;
  final ApprovalTemplateApprover approver;
  final String? note;
}

class ApprovalTemplateApprover {
  ApprovalTemplateApprover({
    required this.id,
    required this.employeeNo,
    required this.name,
  });

  final int id;
  final String employeeNo;
  final String name;
}

/// 결재 템플릿 생성/수정 입력 모델
class ApprovalTemplateInput {
  ApprovalTemplateInput({
    this.code,
    required this.name,
    this.description,
    this.note,
    this.isActive = true,
    this.createdById,
  });

  final String? code;
  final String name;
  final String? description;
  final String? note;
  final bool isActive;
  final int? createdById;

  Map<String, dynamic> toCreatePayload() {
    return {
      if (code != null) 'template_code': code,
      'template_name': name,
      if (description != null && description!.trim().isNotEmpty)
        'description': description,
      if (note != null && note!.trim().isNotEmpty) 'note': note,
      'is_active': isActive,
      if (createdById != null) 'created_by_id': createdById,
    };
  }

  Map<String, dynamic> toUpdatePayload(int id) {
    final payload = <String, dynamic>{
      'id': id,
      'template_name': name,
      'is_active': isActive,
    };
    if (description != null && description!.trim().isNotEmpty) {
      payload['description'] = description;
    }
    if (note != null && note!.trim().isNotEmpty) {
      payload['note'] = note;
    }
    return payload;
  }
}

/// 템플릿 단계 입력 모델
class ApprovalTemplateStepInput {
  ApprovalTemplateStepInput({
    this.id,
    required this.stepOrder,
    required this.approverId,
    this.note,
  });

  final int? id;
  final int stepOrder;
  final int approverId;
  final String? note;

  Map<String, dynamic> toJson({bool includeId = true}) {
    return {
      if (includeId && id != null) 'id': id,
      'step_order': stepOrder,
      'approver_id': approverId,
      if (note != null && note!.trim().isNotEmpty) 'note': note,
    };
  }
}
