// lib/models/note_item_photo.dart

import 'dart:io';
import 'package:latlong2/latlong.dart';

class NoteItemPhoto {
  final int? id;
  final int noteItemId;
  final String localPath; // teljes elérési út a fájlhoz
  final String? storagePath; // Supabase Storage (megosztáskor)
  final String caption; // opcionális megjegyzés
  final double? latitude; // ahol a fotó készült
  final double? longitude;
  final DateTime createdAt;

  const NoteItemPhoto({
    this.id,
    required this.noteItemId,
    required this.localPath,
    this.storagePath,
    this.caption = '',
    this.latitude,
    this.longitude,
    required this.createdAt,
  });

  File get file => File(localPath);
  bool get fileExists => file.existsSync();
  bool get isShared => storagePath != null;
  LatLng? get location => latitude != null && longitude != null
      ? LatLng(latitude!, longitude!)
      : null;

  NoteItemPhoto copyWith({
    String? caption,
    String? storagePath,
  }) =>
      NoteItemPhoto(
        id: id,
        noteItemId: noteItemId,
        localPath: localPath,
        storagePath: storagePath ?? this.storagePath,
        caption: caption ?? this.caption,
        latitude: latitude,
        longitude: longitude,
        createdAt: createdAt,
      );

  Map<String, dynamic> toMap() => {
        if (id != null) 'id': id,
        'note_item_id': noteItemId,
        'local_path': localPath,
        'storage_path': storagePath,
        'caption': caption,
        'latitude': latitude,
        'longitude': longitude,
        'created_at': createdAt.toIso8601String(),
      };

  factory NoteItemPhoto.fromMap(Map<String, dynamic> m) => NoteItemPhoto(
        id: m['id'] as int?,
        noteItemId: m['note_item_id'] as int,
        localPath: m['local_path'] as String,
        storagePath: m['storage_path'] as String?,
        caption: m['caption'] as String? ?? '',
        latitude: (m['latitude'] as num?)?.toDouble(),
        longitude: (m['longitude'] as num?)?.toDouble(),
        createdAt: DateTime.parse(m['created_at'] as String),
      );
}
