elusion 8.2.0

Elusion is a modern DataFrame / Data Engineering / Data Analysis library that combines the familiarity of DataFrame operations (like those in PySpark, Pandas, and Polars) with the power of SQL query building. It provides flexible query construction without enforcing strict operation ordering, enabling developers to write intuitive and maintainable data transformations.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
#[cfg(feature = "ftp")]
use std::time::Duration;
#[cfg(feature = "ftp")]
use suppaftp::{FtpStream, Mode};
#[cfg(feature = "ftp")]
use suppaftp::{NativeTlsFtpStream, NativeTlsConnector};
#[cfg(feature = "ftp")]
use suppaftp::native_tls::TlsConnector;
#[cfg(feature = "ftp")]
use crate::prelude::*;
#[cfg(feature = "ftp")]
use crate::ElusionError;
#[cfg(feature = "ftp")]
use crate::ElusionResult;
#[cfg(feature = "ftp")]
use crate::CustomDataFrame;

/// Internal FTP connection
#[cfg(feature = "ftp")]
pub struct FtpConnection {
    host: String,
    port: u16,
    username: String,
    password: String,
    use_tls: bool,
    passive_mode: bool,
    timeout: Duration,
    working_directory: Option<String>,
}
#[cfg(feature = "ftp")]
impl FtpConnection {
    pub fn new(
        host: String,
        username: String,
        password: String,
    ) -> Self {
        Self {
            host,
            port: 21, 
            username,
            password,
            use_tls: false,
            passive_mode: true,  
            timeout: Duration::from_secs(30),
            working_directory: None,
        }
    }

    pub fn with_tls(mut self) -> Self {
        self.use_tls = true;
        self
    }

    pub fn with_port(mut self, port: u16) -> Self {
        self.port = port;
        self
    }

    pub fn with_directory(mut self, working_directory: String) -> Self {
        self.working_directory = Some(working_directory);
        self
    }

    pub fn with_passive_mode(mut self, passive: bool) -> Self {
        self.passive_mode = passive;
        self
    }

    pub fn with_timeout(mut self, timeout_secs: u64) -> Self {
        self.timeout = Duration::from_secs(timeout_secs);
        self
    }

    pub fn connect(&self) -> ElusionResult<FtpStreamEnum> {
        print!("🔌 Connecting to FTP server... \n");
        let address = format!("{}:{}", self.host, self.port);

        if self.use_tls {
            // For TLS connections, use NativeTlsFtpStream directly
            let ftp_stream = NativeTlsFtpStream::connect(&address)
                .map_err(|e| ElusionError::Custom(format!("Failed to connect to FTP server at {}: {}", address, e)))?;

            // Create TLS connector
            let tls_connector = TlsConnector::builder()
                .danger_accept_invalid_certs(true)
                .build()
                .map_err(|e| ElusionError::Custom(format!("Failed to create TLS connector: {}", e)))?;
            
            let native_tls_connector = NativeTlsConnector::from(tls_connector);
            
            // Upgrade to secure connection
            let mut ftp_stream = ftp_stream
                .into_secure(native_tls_connector, &self.host)
                .map_err(|e| ElusionError::Custom(format!("Failed to establish secure TLS connection: {}", e)))?;

            ftp_stream
                .login(&self.username, &self.password)
                .map_err(|e| ElusionError::Custom(format!("Failed to authenticate with FTP server: {}", e)))?;

            // Set transfer mode (modifies in place, returns ())
            if self.passive_mode {
                ftp_stream.set_mode(Mode::Passive);
            } else {
                ftp_stream.set_mode(Mode::Active);
            }

            // Change to working directory if specified
            if let Some(dir) = &self.working_directory {
                ftp_stream.cwd(dir)
                    .map_err(|e| ElusionError::Custom(format!("Failed to change to working directory {}: {}", dir, e)))?;
            }

            Ok(FtpStreamEnum::Tls(ftp_stream))
        } else {
            // regular (non-TLS) connection
            let mut ftp_stream = FtpStream::connect(&address)
                .map_err(|e| ElusionError::Custom(format!("Failed to connect to FTP server at {}: {}", address, e)))?;

            // Login
            ftp_stream
                .login(&self.username, &self.password)
                .map_err(|e| ElusionError::Custom(format!("Failed to authenticate with FTP server: {}", e)))?;

            // Set transfer mode (modifies in place, returns ())
            if self.passive_mode {
                ftp_stream.set_mode(Mode::Passive);
            } else {
                ftp_stream.set_mode(Mode::Active);
            }

            // Change to working directory if specified
            if let Some(dir) = &self.working_directory {
                ftp_stream.cwd(dir)
                    .map_err(|e| ElusionError::Custom(format!("Failed to change to working directory {}: {}", dir, e)))?;
            }
            print!("✅ Connected and authenticated successfully to FTP Server\n");
            Ok(FtpStreamEnum::Plain(ftp_stream))
        }
    }

    pub fn download_to_temp(&self, remote_path: &str) -> ElusionResult<String> {
        let mut ftp = self.connect()?;
        
        // Extract filename from path
        let filename = remote_path.split('/').last().unwrap_or(remote_path);
        
        let temp_dir = std::env::temp_dir();
        let temp_path = temp_dir.join(format!("elusion_ftp_{}", filename));
        
        let file = std::fs::File::create(&temp_path)
            .map_err(|e| ElusionError::Custom(format!("Failed to create temporary file {:?}: {}", temp_path, e)))?;
        let mut writer = BufWriter::new(file);
        
        match &mut ftp {
            FtpStreamEnum::Plain(stream) => {
                stream.retr(remote_path, |reader| {
                    std::io::copy(reader, &mut writer)
                        .map(|_| ())
                        .map_err(suppaftp::FtpError::ConnectionError)
                })
                .map_err(|e| ElusionError::Custom(format!("Failed to download file {}: {}", remote_path, e)))?;
            },
            FtpStreamEnum::Tls(stream) => {
                stream.retr(remote_path, |reader| {
                    std::io::copy(reader, &mut writer)
                        .map(|_| ())
                        .map_err(suppaftp::FtpError::ConnectionError)
                })
                .map_err(|e| ElusionError::Custom(format!("Failed to download file {}: {}", remote_path, e)))?;
            }
        }
        
        ftp.quit().ok();
        Ok(temp_path.to_string_lossy().to_string())
    }

    pub fn upload_file(&self, local_path: &LocalPath, remote_path: &str) -> ElusionResult<()> {
        let mut ftp = self.connect()?;
        
        let file = std::fs::File::open(local_path)
            .map_err(|e| ElusionError::Custom(format!("Failed to open local file {:?}: {}", local_path, e)))?;
        let mut reader = BufReader::new(file);
        
        match &mut ftp {
            FtpStreamEnum::Plain(stream) => {
                stream.put_file(remote_path, &mut reader)
                    .map_err(|e| ElusionError::Custom(format!("Failed to upload file to {}: {}", remote_path, e)))?;
            },
            FtpStreamEnum::Tls(stream) => {
                stream.put_file(remote_path, &mut reader)
                    .map_err(|e| ElusionError::Custom(format!("Failed to upload file to {}: {}", remote_path, e)))?;
            }
        }
        
        ftp.quit().ok();
        Ok(())
    }

    pub fn list_files(&self, path: Option<&str>) -> ElusionResult<Vec<String>> {
        let mut ftp = self.connect()?;
        
        if let Some(dir) = path {
            match &mut ftp {
                FtpStreamEnum::Plain(stream) => {
                    stream.cwd(dir)
                        .map_err(|e| ElusionError::Custom(format!("Failed to change directory to {}: {}", dir, e)))?;
                },
                FtpStreamEnum::Tls(stream) => {
                    stream.cwd(dir)
                        .map_err(|e| ElusionError::Custom(format!("Failed to change directory to {}: {}", dir, e)))?;
                }
            }
        }

        let files = match &mut ftp {
            FtpStreamEnum::Plain(stream) => {
                stream.nlst(None)
                    .map_err(|e| ElusionError::Custom(format!("Failed to list files: {}", e)))?
            },
            FtpStreamEnum::Tls(stream) => {
                stream.nlst(None)
                    .map_err(|e| ElusionError::Custom(format!("Failed to list files: {}", e)))?
            }
        };
        
        ftp.quit().ok();
        Ok(files)
    }

    pub fn filter_files_by_extension(&self, path: Option<&str>, extensions: &[&str]) -> ElusionResult<Vec<String>> {
        let all_files = self.list_files(path)?;
        
        let filtered: Vec<String> = all_files
            .into_iter()
            .filter(|file| {
                extensions.iter().any(|ext| {
                    file.to_lowercase().ends_with(&format!(".{}", ext.to_lowercase()))
                })
            })
            .collect();
        
        Ok(filtered)
    }
}

// Enum to handle both plain and TLS FTP streams
#[cfg(feature = "ftp")]
pub enum FtpStreamEnum {
    Plain(FtpStream),
    Tls(NativeTlsFtpStream),
}
#[cfg(feature = "ftp")]
impl FtpStreamEnum {
    pub fn quit(&mut self) -> suppaftp::FtpResult<()> {
        match self {
            FtpStreamEnum::Plain(stream) => stream.quit(),
            FtpStreamEnum::Tls(stream) => stream.quit(),
        }
    }

   pub fn size(&mut self, remote_path: &str) -> suppaftp::FtpResult<usize> {
        match self {
            FtpStreamEnum::Plain(stream) => stream.size(remote_path),
            FtpStreamEnum::Tls(stream) => stream.size(remote_path),
        }
    }

    pub fn rm(&mut self, remote_path: &str) -> suppaftp::FtpResult<()> {
        match self {
            FtpStreamEnum::Plain(stream) => stream.rm(remote_path),
            FtpStreamEnum::Tls(stream) => stream.rm(remote_path),
        }
    }

    pub fn mkdir(&mut self, remote_path: &str) -> suppaftp::FtpResult<()> {
        match self {
            FtpStreamEnum::Plain(stream) => stream.mkdir(remote_path),
            FtpStreamEnum::Tls(stream) => stream.mkdir(remote_path),
        }
    }
}

// FOLDER implementation
#[cfg(feature = "ftp")]
pub async fn from_ftp_folder_impl(
    server: &str,
    username: &str,
    password: &str,
    port: Option<u16>,
    use_tls: bool,
    folder_path: &str,
    file_extensions: Option<Vec<&str>>,
    result_alias: &str,
) -> ElusionResult<CustomDataFrame> {
    // List files in the folder
    let files = FtpUtils::list_files(
        server,
        username,
        password,
        Some(folder_path),
        port,
        use_tls,
    )?;

    let mut dataframes = Vec::new();

    for file_name in files {
        let file_ext = file_name
            .split('.')
            .last()
            .unwrap_or("")
            .to_lowercase();

        if !["csv", "json", "parquet", "xml", "xlsx", "xls"].contains(&file_ext.as_str()) {
            println!("⏭️ Skipping unsupported file type: {}", file_name);
            continue;
        }

        if let Some(ref extensions) = file_extensions {
            if !extensions.iter().any(|ext| ext.to_lowercase() == file_ext) {
                continue;
            }
        }

        let remote_path = format!("{}/{}", folder_path.trim_end_matches('/'), file_name);

        let load_result = if use_tls {
            CustomDataFrame::from_ftps(
                server,
                username,
                password,
                &remote_path,
                &format!("ftp_file_{}", dataframes.len()),
            ).await
        } else {
            CustomDataFrame::from_ftp(
                server,
                username,
                password,
                &remote_path,
                &format!("ftp_file_{}", dataframes.len()),
            ).await
        };

        match load_result {
            Ok(df) => {
                println!("✅ Loaded file: {}", file_name);
                dataframes.push(df);
            },
            Err(e) => {
                eprintln!("⚠️ Failed to load file {}: {}", file_name, e);
                continue;
            }
        }
    }

    if dataframes.is_empty() {
        return Err(ElusionError::InvalidOperation {
            operation: "FTP Folder Loading".to_string(),
            reason: "No supported files found or all files failed to load".to_string(),
            suggestion: "💡 Check folder path and ensure it contains CSV, Excel, JSON, Parquet, or XML files".to_string(),
        });
    }

    // If only one file, return it directly
    if dataframes.len() == 1 {
        println!("📄 Single file loaded, returning as-is");
        return dataframes.into_iter().next().unwrap().elusion(result_alias).await;
    }

    // Check schema compatibility by column names AND types
    println!("🔍 Checking schema compatibility for {} files (names + types)...", dataframes.len());

    let first_schema = dataframes[0].df.schema();
    let mut compatible_schemas = true;
    let mut schema_issues = Vec::new();

    // Print first file schema for reference
    println!("📋 File 1 schema:");
    for (i, field) in first_schema.fields().iter().enumerate() {
        println!("   Column {}: '{}' ({})", i + 1, field.name(), field.data_type());
    }

    for (file_idx, df) in dataframes.iter().enumerate().skip(1) {
        let current_schema = df.df.schema();

        println!("📋 File {} schema:", file_idx + 1);
        for (i, field) in current_schema.fields().iter().enumerate() {
            println!("   Column {}: '{}' ({})", i + 1, field.name(), field.data_type());
        }

        // Check if column count matches
        if first_schema.fields().len() != current_schema.fields().len() {
                compatible_schemas = false;
                schema_issues.push(format!("File {} has {} columns, but first file has {}", 
                    file_idx + 1, current_schema.fields().len(), first_schema.fields().len()));
                continue;
            }

        // Check if column names and types match (case insensitive names)
        for (col_idx, first_field) in first_schema.fields().iter().enumerate() {
            if let Some(current_field) = current_schema.fields().get(col_idx) {
                if first_field.name().to_lowercase() != current_field.name().to_lowercase() {
                    compatible_schemas = false;
                    schema_issues.push(format!("File {} column {} name is '{}', but first file has '{}'", 
                        file_idx + 1, col_idx + 1, current_field.name(), first_field.name()));
                }

                if first_field.data_type() != current_field.data_type() {
                    compatible_schemas = false;
                    schema_issues.push(format!("File {} column {} ('{}') type is {:?}, but first file has {:?}", 
                        file_idx + 1, col_idx + 1, current_field.name(), 
                        current_field.data_type(), first_field.data_type()));
                }
            }
        }
    }

    if !compatible_schemas {
        println!("⚠️ Schema compatibility issues found:");
        for issue in &schema_issues {
            println!("   {}", issue);
        }

        println!("🔧 Reordering columns by name to match first file...");

        let first_file_columns: Vec<String> = first_schema.fields()
            .iter()
            .map(|field| field.name().clone())
            .collect();

        println!("📋 Target column order: {:?}", first_file_columns);

        let mut reordered_dataframes = Vec::new();

        for (i, df) in dataframes.clone().into_iter().enumerate() {
                let column_refs: Vec<&str> = first_file_columns.iter().map(|s| s.as_str()).collect();
                let reordered_df = df.select_vec(column_refs);
                
                let temp_alias = format!("reordered_file_{}", i + 1);
                match reordered_df.elusion(&temp_alias).await {
                    Ok(standardized_df) => {
                        println!("✅ Reordered file {} columns", i + 1);
                        reordered_dataframes.push(standardized_df);
                    },
                    Err(e) => {
                        eprintln!("⚠️ Failed to reorder file {} columns: {}", i + 1, e);
                        continue;
                    }
                }
            }

        if reordered_dataframes.is_empty() {
            println!("📄 Column reordering failed, returning first file only");
            return dataframes.into_iter().next().unwrap().elusion(result_alias).await;
        }

        dataframes = reordered_dataframes;
        println!("✅ All files reordered to match first file column order");
    } else {
        println!("✅ All schemas are compatible!");
    }

    // Union the compatible dataframes
    println!("🔗 Unioning {} files with compatible schemas...", dataframes.len());

    let total_files = dataframes.len();
     let mut result = dataframes.clone().into_iter().next().unwrap();

    for (i, df) in dataframes.into_iter().enumerate().skip(1) {
            result = result.union_all(df).await
                .map_err(|e| ElusionError::InvalidOperation {
                    operation: "SharePoint Folder Union with Filename".to_string(),
                    reason: format!("Failed to union file {}: {}", i + 1, e),
                    suggestion: "💡 Check that all files have compatible schemas".to_string(),
                })?;
            
            println!("✅ Unioned file {}/{}", i + 1, total_files - 1);
        }

    println!("🎉 Successfully combined {} files using UNION ALL", total_files);

    result.elusion(result_alias).await
}

#[cfg(feature = "ftp")]
pub async fn from_ftp_folder_with_filename_column_impl(
    server: &str,
    username: &str,
    password: &str,
    port: Option<u16>,
    use_tls: bool,
    folder_path: &str,
    file_extensions: Option<Vec<&str>>,
    result_alias: &str,
) -> ElusionResult<CustomDataFrame> {
    // List files in the folder
    let files = FtpUtils::list_files(
        server,
        username,
        password,
        Some(folder_path),
        port,
        use_tls,
    )?;

    let mut dataframes = Vec::new();

    for file_name in files {
        let file_ext = file_name
            .split('.')
            .last()
            .unwrap_or("")
            .to_lowercase();

        if !["csv", "json", "parquet", "xml", "xlsx", "xls"].contains(&file_ext.as_str()) {
            println!("⏭️ Skipping unsupported file type: {}", file_name);
            continue;
        }

        if let Some(ref extensions) = file_extensions {
            if !extensions.iter().any(|ext| ext.to_lowercase() == file_ext) {
                continue;
            }
        }

        let remote_path = format!("{}/{}", folder_path.trim_end_matches('/'), file_name);

        let load_result = if use_tls {
            CustomDataFrame::from_ftps(
                server,
                username,
                password,
                &remote_path,
                &format!("ftp_file_{}", dataframes.len()),
            ).await
        } else {
            CustomDataFrame::from_ftp(
                server,
                username,
                password,
                &remote_path,
                &format!("ftp_file_{}", dataframes.len()),
            ).await
        };

        let mut loaded_df = match load_result {
            Ok(df) => {
                println!("✅ Loaded file: {}", file_name);
                df
            },
            Err(e) => {
                eprintln!("⚠️ Failed to load file {}: {}", file_name, e);
                continue;
            }
        };

        // Add filename as a new column using select with literal value
        loaded_df = loaded_df.select_vec(vec![
            &format!("'{}' AS filename_added", file_name), 
            "*"
        ]);
        
        // Execute the selection to create the dataframe with filename column
        let temp_alias = format!("file_with_filename_{}", dataframes.len());
        loaded_df = match loaded_df.elusion(&temp_alias).await {
            Ok(filename_df) => {
                println!("✅ Added filename column to {}", file_name);
                filename_df
            },
            Err(e) => {
                eprintln!("⚠️ Failed to add filename to {}: {}", file_name, e);
                continue;
            }
        };

        dataframes.push(loaded_df);
    }

    if dataframes.is_empty() {
        return Err(ElusionError::InvalidOperation {
            operation: "FTP Folder Loading with Filename".to_string(),
            reason: "No supported files found or all files failed to load".to_string(),
            suggestion: "💡 Check folder path and ensure it contains CSV, Excel, JSON, Parquet, or XML files".to_string(),
        });
    }

    // If only one file, return it directly
    if dataframes.len() == 1 {
        println!("📄 Single file loaded, returning as-is");
        return dataframes.into_iter().next().unwrap().elusion(result_alias).await;
    }

    // Check schema compatibility by column names AND types
    println!("🔍 Checking schema compatibility for {} files (names + types)...", dataframes.len());

    let first_schema = dataframes[0].df.schema();
    let mut compatible_schemas = true;
    let mut schema_issues = Vec::new();

    // Print first file schema for reference
    println!("📋 File 1 schema:");
    for (i, field) in first_schema.fields().iter().enumerate() {
        println!("   Column {}: '{}' ({})", i + 1, field.name(), field.data_type());
    }

    for (file_idx, df) in dataframes.iter().enumerate().skip(1) {
        let current_schema = df.df.schema();

        println!("📋 File {} schema:", file_idx + 1);
        for (i, field) in current_schema.fields().iter().enumerate() {
            println!("   Column {}: '{}' ({})", i + 1, field.name(), field.data_type());
        }

        // Check if column count matches
        if first_schema.fields().len() != current_schema.fields().len() {
                compatible_schemas = false;
                schema_issues.push(format!("File {} has {} columns, but first file has {}", 
                    file_idx + 1, current_schema.fields().len(), first_schema.fields().len()));
                continue;
            }

        // Check if column names and types match (case insensitive names)
        for (col_idx, first_field) in first_schema.fields().iter().enumerate() {
            if let Some(current_field) = current_schema.fields().get(col_idx) {
                if first_field.name().to_lowercase() != current_field.name().to_lowercase() {
                    compatible_schemas = false;
                    schema_issues.push(format!("File {} column {} name is '{}', but first file has '{}'", 
                        file_idx + 1, col_idx + 1, current_field.name(), first_field.name()));
                }

                if first_field.data_type() != current_field.data_type() {
                    compatible_schemas = false;
                    schema_issues.push(format!("File {} column {} ('{}') type is {:?}, but first file has {:?}", 
                        file_idx + 1, col_idx + 1, current_field.name(), 
                        current_field.data_type(), first_field.data_type()));
                }
            }
        }
    }

    if !compatible_schemas {
        println!("⚠️ Schema compatibility issues found:");
        for issue in &schema_issues {
            println!("   {}", issue);
        }

        println!("🔧 Reordering columns by name to match first file...");

        let first_file_columns: Vec<String> = first_schema.fields()
            .iter()
            .map(|field| field.name().clone())
            .collect();

        println!("📋 Target column order: {:?}", first_file_columns);

        let mut reordered_dataframes = Vec::new();

        for (i, df) in dataframes.clone().into_iter().enumerate() {
                let column_refs: Vec<&str> = first_file_columns.iter().map(|s| s.as_str()).collect();
                let reordered_df = df.select_vec(column_refs);
                
                let temp_alias = format!("reordered_file_{}", i + 1);
                match reordered_df.elusion(&temp_alias).await {
                    Ok(standardized_df) => {
                        println!("✅ Reordered file {} columns", i + 1);
                        reordered_dataframes.push(standardized_df);
                    },
                    Err(e) => {
                        eprintln!("⚠️ Failed to reorder file {} columns: {}", i + 1, e);
                        continue;
                    }
                }
            }

        if reordered_dataframes.is_empty() {
            println!("📄 Column reordering failed, returning first file only");
            return dataframes.into_iter().next().unwrap().elusion(result_alias).await;
        }

        dataframes = reordered_dataframes;
        println!("✅ All files reordered to match first file column order");
    } else {
        println!("✅ All schemas are compatible!");
    }

    // Union the compatible dataframes
    println!("🔗 Unioning {} files with compatible schemas...", dataframes.len());

    let total_files = dataframes.len();
     let mut result = dataframes.clone().into_iter().next().unwrap();

    for (i, df) in dataframes.into_iter().enumerate().skip(1) {
            result = result.union_all(df).await
                .map_err(|e| ElusionError::InvalidOperation {
                    operation: "FTP Folder Union with Filename".to_string(),
                    reason: format!("Failed to union file {}: {}", i + 1, e),
                    suggestion: "💡 Check that all files have compatible schemas".to_string(),
                })?;
            
            println!("✅ Unioned file {}/{}", i + 1, total_files - 1);
        }

    println!("🎉 Successfully combined {} files using UNION ALL", total_files);

    result.elusion(result_alias).await
}

// impls 

    /// Load data from FTP server
    #[cfg(feature = "ftp")]
    pub async fn from_ftp_impl(
        server: &str,
        username: &str,
        password: &str,
        remote_path: &str,
        alias: &str
    ) -> ElusionResult<CustomDataFrame> {
        let connection = FtpConnection::new(
            server.to_string(),
            username.to_string(),
            password.to_string()
        );
        
        let temp_path = connection.download_to_temp(remote_path)
            .map_err(|e| ElusionError::Custom(format!("Failed to download from FTP: {}", e)))?;
        
        let df = CustomDataFrame::new(&temp_path, alias).await?;
        
        std::fs::remove_file(&temp_path).ok();
        
        Ok(df)
    }

    /// Load data from FTPS server (secure FTP)
    #[cfg(feature = "ftp")]
    pub async fn from_ftps_impl(
        server: &str,
        username: &str,
        password: &str,
        remote_path: &str,
        alias: &str
    ) -> ElusionResult<CustomDataFrame> {
        let connection = FtpConnection::new(
            server.to_string(),
            username.to_string(),
            password.to_string()
        ).with_tls();

        let temp_path = connection.download_to_temp(remote_path)
            .map_err(|e| ElusionError::Custom(format!("Failed to download from FTPS: {}", e)))?;
        
        let df = CustomDataFrame::new(&temp_path, alias).await?;
        
        std::fs::remove_file(&temp_path).ok();
        
        Ok(df)
    }

    /// Load data from FTP server with custom port
    #[cfg(feature = "ftp")]
    pub async fn from_ftp_with_port_impl(
        server: &str,
        port: u16,
        username: &str,
        password: &str,
        remote_path: &str,
        alias: &str
    ) -> ElusionResult<CustomDataFrame> {
        let connection = FtpConnection::new(
            server.to_string(),
            username.to_string(),
            password.to_string()
        ).with_port(port);
        
        let temp_path = connection.download_to_temp(remote_path)
            .map_err(|e| ElusionError::Custom(format!("Failed to download from FTP: {}", e)))?;
        
        let df = CustomDataFrame::new(&temp_path, alias).await?;
        
        std::fs::remove_file(&temp_path).ok();
        
        Ok(df)
    }

    /// Load data from FTP server with working directory
    #[cfg(feature = "ftp")]
    pub async fn from_ftp_with_directory_impl(
        server: &str,
        username: &str,
        password: &str,
        directory: &str,
        remote_path: &str,
        alias: &str
    ) -> ElusionResult<CustomDataFrame> {
        let connection = FtpConnection::new(
            server.to_string(),
            username.to_string(),
            password.to_string()
        ).with_directory(directory.to_string());
        
        let temp_path = connection.download_to_temp(remote_path)
            .map_err(|e| ElusionError::Custom(format!("Failed to download from FTP: {}", e)))?;
        
        let df = CustomDataFrame::new(&temp_path, alias).await?;
        
        std::fs::remove_file(&temp_path).ok();
        
        Ok(df)
    }

    /// Write DataFrame result to FTP server as CSV
    #[cfg(feature = "ftp")]
    pub async fn write_csv_to_ftp_impl(
        df: &CustomDataFrame,
        server: &str,
        username: &str,
        password: &str,
        remote_path: &str
    ) -> ElusionResult<()> {
        let connection = FtpConnection::new(
            server.to_string(),
            username.to_string(),
            password.to_string()
        );
        
        let temp_dir = std::env::temp_dir();
        let temp_filename = format!("elusion_ftp_temp_{}.csv", Utc::now().timestamp());
        let temp_path = temp_dir.join(&temp_filename);
        
        let csv_options = CsvWriteOptions {
            delimiter: b',',
            escape: b'\\',
            quote: b'"',
            double_quote: false,
            null_value: "NULL".to_string(),
        };
        
        df.write_to_csv("overwrite", &temp_path.to_string_lossy(), csv_options).await?;
        
        connection.upload_file(&temp_path, remote_path)
            .map_err(|e| ElusionError::Custom(format!("Failed to upload to FTP: {}", e)))?;
        
        std::fs::remove_file(&temp_path).ok();
        
        Ok(())
    }

    /// Write DataFrame result to FTP server as Excel
    #[cfg(feature = "ftp")]
    pub async fn write_excel_to_ftp_impl(
        df: &CustomDataFrame,
        server: &str,
        username: &str,
        password: &str,
        remote_path: &str,
        sheet_name: Option<&str>
    ) -> ElusionResult<()> {
        let connection = FtpConnection::new(
            server.to_string(),
            username.to_string(),
            password.to_string()
        );
 
        let temp_dir = std::env::temp_dir();
        let temp_filename = format!("elusion_ftp_temp_{}.xlsx", Utc::now().timestamp());
        let temp_path = temp_dir.join(&temp_filename);
        
        df.write_to_excel(&temp_path.to_string_lossy(), sheet_name).await?;
        
        connection.upload_file(&temp_path, remote_path)
            .map_err(|e| ElusionError::Custom(format!("Failed to upload to FTP: {}", e)))?;
        
        std::fs::remove_file(&temp_path).ok();
        
        Ok(())
    }

    /// Write DataFrame result to FTP server as Parquet
    #[cfg(feature = "ftp")]
    pub async fn write_parquet_to_ftp_impl(
        df: &CustomDataFrame,
        server: &str,
        username: &str,
        password: &str,
        remote_path: &str
    ) -> ElusionResult<()> {
        let connection = FtpConnection::new(
            server.to_string(),
            username.to_string(),
            password.to_string()
        );

        let temp_dir = std::env::temp_dir();
        let temp_filename = format!("elusion_ftp_temp_{}.parquet", Utc::now().timestamp());
        let temp_path = temp_dir.join(&temp_filename);
        
        df.write_to_parquet("overwrite", &temp_path.to_string_lossy(), None).await?;
        
        connection.upload_file(&temp_path, remote_path)
            .map_err(|e| ElusionError::Custom(format!("Failed to upload to FTP: {}", e)))?;
        
        std::fs::remove_file(&temp_path).ok();
        
        Ok(())
    }

    /// Write DataFrame result to FTP server as JSON
    #[cfg(feature = "ftp")]
    pub async fn write_json_to_ftp_impl(
        df: &CustomDataFrame,
        server: &str,
        username: &str,
        password: &str,
        remote_path: &str,
        pretty: bool
    ) -> ElusionResult<()> {
        let connection = FtpConnection::new(
            server.to_string(),
            username.to_string(),
            password.to_string()
        );
        
        let temp_dir = std::env::temp_dir();
        let temp_filename = format!("elusion_ftp_temp_{}.json", Utc::now().timestamp());
        let temp_path = temp_dir.join(&temp_filename);
        
        df.write_to_json(&temp_path.to_string_lossy(), pretty).await?;

        connection.upload_file(&temp_path, remote_path)
            .map_err(|e| ElusionError::Custom(format!("Failed to upload to FTP: {}", e)))?;
        
        std::fs::remove_file(&temp_path).ok();
        
        Ok(())
    }


    
// ---- UTILS

#[cfg(feature = "ftp")]
pub struct FtpUtils;

#[cfg(feature = "ftp")]
impl FtpUtils {
    pub fn list_files(
        server: &str,
        username: &str,
        password: &str,
        path: Option<&str>,
        port: Option<u16>,
        use_tls: bool,
    ) -> ElusionResult<Vec<String>> {
        let mut connection = FtpConnection::new(
            server.to_string(),
            username.to_string(),
            password.to_string(),
        );
        if let Some(p) = port {
            connection = connection.with_port(p);
        }
        if use_tls {
            connection = connection.with_tls();
        }
        connection.list_files(path)
    }
}