aipack 0.8.26

Command Agent runner to accelerate production coding with genai.
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
//! Defines the `stats`, `load`, `exists`, `info`, `list`, `list_load`, and `first` functions for the `aip.file` Lua module.
//!
//! ---
//!
//! ## Lua documentation for `aip.file` read functions
//!
//! ### Functions
//!
//! - `aip.file.stats(include_globs: string | string[] | nil, options?: {base_dir?: string, absolute?: boolean}): FileStats | nil`
//! - `aip.file.load(rel_path: string, options?: {base_dir: string}): FileRecord`
//! - `aip.file.exists(path: string): boolean`
//! - `aip.file.info(path: string): FileInfo | nil`
//! - `aip.file.list(include_globs: string | string[], options?: {base_dir?: string, absolute?: boolean, with_meta?: boolean}): FileInfo[]`
//! - `aip.file.list_load(include_globs: string | string[], options?: {base_dir?: string, absolute?: boolean}): FileRecord[]`
//! - `aip.file.first(include_globs: string | string[], options?: {base_dir?: string, absolute?: boolean}): FileInfo | nil`

use crate::dir_context::PathResolver;
use crate::runtime::Runtime;
use crate::script::LuaValueExt;
use crate::script::aip_modules::support::{
	base_dir_and_globs, compute_base_dir, create_file_records, list_files_with_options,
};
use crate::script::support::into_option_string;
use crate::support::AsStrsExt;
use crate::types::{FileInfo, FileRecord, FileStats};
use mlua::{IntoLua, Lua, Value};
use simple_fs::{SMeta, SPath, iter_files};

/// ## Lua Documentation
///
/// Calculates aggregate statistics for a set of files matching glob patterns.
///
/// ```lua
/// -- API Signature
/// aip.file.stats(
///   include_globs: string | list<string> | nil,
///   options?: {
///     base_dir?: string,
///     absolute?: boolean
///   }
/// ): FileStats | nil
/// ```
///
/// Finds files matching the `include_globs` patterns within the specified `base_dir` (or workspace root)
/// and returns aggregate statistics about these files in a `FileStats` object.
/// If `include_globs` is `nil` or no files match the patterns, returns `nil`.
///
/// ### Arguments
///
/// - `include_globs: string | list<string> | nil` - A single glob pattern string, a Lua list (table) of glob pattern strings, or `nil`.
///   If `nil`, the function returns `nil`.
///   Globs can include standard wildcards (`*`, `?`, `**`, `[]`). Pack references (e.g., `ns@pack/**/*.md`) are supported.
///   Note: Common build/dependency folders (e.g., `target/`, `node_modules/`, `.build/`, `__pycache__/`)
///
///   are excluded by default unless explicitly matched by the globs.
/// - `options?: table` (optional) - A table containing options:
///   - `base_dir?: string` (optional): The directory relative to which the `include_globs` are applied.
///     Defaults to the workspace root. Pack references (e.g., `ns@pack/`) are supported.
///   - `absolute?: boolean` (optional): Affects how files are resolved internally, but the statistics remain the same regardless.
///
/// ### Returns
///
/// - `FileStats`: A `FileStats` object containing aggregate statistics about the matching files.
/// - `nil` if `include_globs` is `nil`
///
/// If no files if ound a FileStats will all 0 will be returned.
///
/// ### Example
///
/// ```lua
/// -- Get statistics for all Markdown files in the 'docs' directory
/// local stats = aip.file.stats("*.md", { base_dir = "docs" })
/// if stats then
///   print("Number of files:", stats.number_of_files)
///   print("Total size:", stats.total_size)
///   print("First created:", stats.ctime_first)
///   print("Last modified:", stats.mtime_last)
/// end
///
/// -- Get statistics for all '.aip' files in a specific pack
/// local agent_stats = aip.file.stats("**/*.aip", { base_dir = "ns@pack/" })
/// if agent_stats then
///   print("Total agent files:", agent_stats.number_of_files)
/// end
///
/// -- Nil globs return nil
/// local nil_stats = aip.file.stats(nil)
/// print(nil_stats) -- Output: nil
/// ```
///
/// ### Error
///
/// Returns an error if:
/// - `include_globs` is not a string, a list of strings, or `nil`.
/// - `base_dir` cannot be resolved (e.g., invalid pack reference).
/// - An error occurs during file system traversal or glob matching.
///
pub(super) fn file_stats(
	lua: &Lua,
	runtime: &Runtime,
	include_globs: Value,
	options: Option<Value>,
) -> mlua::Result<Value> {
	// Handle nil globs
	if include_globs.is_nil() {
		return Ok(Value::Nil);
	}

	let (base_path, include_globs) = base_dir_and_globs(runtime, include_globs, options.as_ref())?;
	let absolute = options.x_get_bool("absolute").unwrap_or(false);

	let file_refs = list_files_with_options(runtime, base_path.as_ref(), &include_globs.x_as_strs(), absolute, false)?;

	if file_refs.is_empty() {
		return FileStats::default().into_lua(lua);
	}

	// We need metadata to compute stats
	let smetas: Vec<&SMeta> = file_refs.iter().filter_map(|f_ref| f_ref.meta()).collect();

	// Compute aggregate statistics
	let mut total_size: u64 = 0;
	let mut number_of_files: u64 = 0;
	let mut ctime_first: Option<i64> = None;
	let mut ctime_last: Option<i64> = None;
	let mut mtime_first: Option<i64> = None;
	let mut mtime_last: Option<i64> = None;

	for smeta in smetas {
		number_of_files += 1;
		total_size += smeta.size;

		let ctime = smeta.created_epoch_us;
		let mtime = smeta.modified_epoch_us;

		ctime_first = Some(match ctime_first {
			Some(v) => v.min(ctime),
			None => ctime,
		});

		ctime_last = Some(match ctime_last {
			Some(v) => v.max(ctime),
			None => ctime,
		});

		mtime_first = Some(match mtime_first {
			Some(v) => v.min(mtime),
			None => mtime,
		});

		mtime_last = Some(match mtime_last {
			Some(v) => v.max(mtime),
			None => mtime,
		});
	}

	let file_stats = FileStats {
		total_size,
		number_of_files,
		ctime_first: ctime_first.unwrap_or(0),
		ctime_last: ctime_last.unwrap_or(0),
		mtime_first: mtime_first.unwrap_or(0),
		mtime_last: mtime_last.unwrap_or(0),
	};

	let res = file_stats.into_lua(lua)?;

	Ok(res)
}

/// ## Lua Documentation
///
/// Loads a [`FileRecord`] with its content.
///
/// ```lua
/// -- API Signature
/// aip.file.load(rel_path: string, options?: {base_dir: string}): FileRecord
/// ```
///
/// Loads the file specified by `rel_path` and returns a `FileRecord` object containing
/// the file's metadata and its content.
///
/// ### Arguments
///
/// - `rel_path: string` - The path to the file, relative to the `base_dir` or workspace root.
/// - `options?: table` - An optional table containing:
///   - `base_dir: string` (optional): The base directory from which `rel_path` is resolved. Defaults to the workspace root. Pack references (e.g., `ns@pack/`) can be used.
///
/// ### Returns
///
/// - `FileRecord`: A [`FileRecord`] object.
///
/// ### Example
///
/// ```lua
/// local readme = aip.file.load("doc/README.md")
/// print(readme.path)    -- Output: "doc/README.md"
/// print(readme.name)    -- Output: "README.md"
/// print(#readme.content) -- Output: <length of content>
///
/// local agent_file = aip.file.load("agent.aip", { base_dir = "ns@pack/" })
/// print(agent_file.path) -- Output: "agent.aip" (relative to the resolved base_dir)
/// ```
///
/// ### Error
///
/// Returns an error if:
/// - The `base_dir` cannot be resolved (e.g., invalid pack reference).
/// - The final file path cannot be resolved.
/// - The file does not exist or cannot be read.
/// - Metadata cannot be retrieved.
///
/// ```ts
/// {
///   error: string // Error message
/// }
/// ```
pub(super) fn file_load(
	lua: &Lua,
	runtime: &Runtime,
	rel_path: String,
	options: Option<Value>,
) -> mlua::Result<mlua::Value> {
	let dir_context = runtime.dir_context();
	let base_path = compute_base_dir(runtime, options.as_ref())?;
	let full_path = dir_context.resolve_path(
		runtime.session(),
		(&rel_path).into(),
		PathResolver::WksDir,
		base_path.as_ref(),
	)?;
	let full_path = match (base_path, full_path.is_absolute()) {
		(Some(base_path), false) => base_path.join(full_path),
		_ => full_path,
	};

	let rel_path = SPath::new(rel_path);

	let file_record = FileRecord::load_from_full_path(runtime.dir_context(), &full_path, rel_path)?;
	let res = file_record.into_lua(lua)?;

	Ok(res)
}

/// ## Lua Documentation
///
/// Checks if a file or directory exists at the given path.
///
/// ```lua
/// -- API Signature
/// aip.file.exists(path: string): boolean
/// ```
///
/// Checks if the file or directory specified by `path` exists. The path is resolved relative to the workspace root.
/// Supports relative paths, absolute paths, and pack references (`ns@pack/...`).
///
/// ### Arguments
///
/// - `path: string`: The path string to check for existence. Can be relative, absolute, or a pack reference.
///
/// ### Returns
///
/// - `boolean`: Returns `true` if a file or directory exists at the specified path, `false` otherwise.
///
/// ### Example
///
/// ```lua
/// if aip.file.exists("README.md") then
///   print("README.md exists.")
/// end
///
/// if aip.file.exists("ns@pack/main.aip") then
///   print("Pack main agent exists.")
/// end
/// ```
///
/// ### Error
///
/// Returns an error if the path string cannot be resolved (e.g., invalid pack reference, invalid path format).
///
/// ```ts
/// {
///   error: string // Error message
/// }
/// ```
pub(super) fn file_exists(_lua: &Lua, runtime: &Runtime, path: String) -> mlua::Result<bool> {
	Ok(crate::script::support::path_exists(runtime, &path))
}

/// ## Lua Documentation
///
/// Retrieves file metadata ([`FileInfo`]) for the specified path.
///
/// ```lua
/// -- API Signature
/// aip.file.info(path: string): FileInfo | nil
/// ```
///
/// If the given `path` exists, this function returns a [`FileInfo`] object
/// containing the file metadata (no content).
/// If the path cannot be resolved or the file does not exist, it returns `nil`.
///
/// ### Arguments
///
/// - `path: string` – The file or directory path. Can be relative, absolute,
///   or use pack references (`ns@pack/...`, `ns@pack$workspace/...`, etc.).
///
/// ### Returns
///
/// - `FileInfo | nil`: Metadata for the file, or `nil` when not found.
///
/// ### Example
///
/// ```lua
/// local meta = aip.file.info("README.md")
/// if meta then
///   print("Size:", meta.size)
/// end
/// ```
///
/// ### Error
///
/// Returns an error only if the path cannot be resolved (invalid pack
/// reference, invalid format, …). If the path resolves successfully but the
/// file does not exist, the function simply returns `nil`.
pub(super) fn file_info(lua: &Lua, runtime: &Runtime, path: Value) -> mlua::Result<Value> {
	let Some(path) = into_option_string(path, "aip.file.info")? else {
		return Ok(Value::Nil);
	};

	// Empty string ‑> nil directly.
	if path.trim().is_empty() {
		return Ok(Value::Nil);
	}

	let rel_path = SPath::new(path);
	let full_path =
		runtime
			.dir_context()
			.resolve_path(runtime.session(), rel_path.clone(), PathResolver::WksDir, None)?;

	if !full_path.is_file() {
		return Ok(Value::Nil);
	}

	// TODO: Might want to put it ~ in case absolute and home based
	let file_info = FileInfo::new(runtime.dir_context(), rel_path, &full_path);
	file_info.into_lua(lua)
}

/// ## Lua Documentation
///
/// Lists file metadata ([`FileInfo`]) matching glob patterns.
///
/// ```lua
/// -- API Signature
/// aip.file.list(
///   include_globs: string | list<string>,
///   options?: {
///     base_dir?: string,
///     absolute?: boolean,
///     with_meta?: boolean
///   }
/// ): list<FileInfo>
/// ```
///
/// Finds files matching the `include_globs` patterns within the specified `base_dir` (or workspace root)
/// and returns a list of `FileInfo` objects containing information about each file (path, name, timestamps, size, etc.),
/// but *not* the file content.
///
/// ### Arguments
///
/// - `include_globs: string | list<string>` - A single glob pattern string or a Lua list (table) of glob pattern strings.
///   Globs can include standard wildcards (`*`, `?`, `**`, `[]`). Pack references (e.g., `ns@pack/**/*.md`) are supported.
///   Note: Common build/dependency folders (e.g., `target/`, `node_modules/`, `.build/`, `__pycache__/`)
///
///   are excluded by default unless explicitly matched by the globs.
/// - `options?: table` (optional) - A table containing options:
///   - `base_dir?: string` (optional): The directory relative to which the `include_globs` are applied.
///     Defaults to the workspace root. Pack references (e.g., `ns@pack/`) are supported.
///   - `absolute?: boolean` (optional): If `true`, the `path` in the returned `FileInfo` objects will be absolute.
///     If `false` (default), the `path` will be relative to the `base_dir`. If a path resolves outside the `base_dir`
///     (e.g., using `../` in globs), it will be returned as an absolute path even if `absolute` is false.
///   - `with_meta?: boolean` (optional): If `false`, the function will skip fetching detailed metadata
///     (`ctime`, `mtime`, `size`) for each file, potentially improving performance
///     if only the path information is needed. Defaults to `true`.
///
/// ### Returns
///
/// - `list<FileInfo>`: A list of [`FileInfo`] objects. Returns an empty list if no files match.
///
/// ### Example
///
/// ```lua
/// -- List all Markdown files in the 'docs' directory (relative paths)
/// local doc_files = aip.file.list("*.md", { base_dir = "docs" })
/// for _, file in ipairs(doc_files) do
///   print(file.path) -- e.g., "guide.md", "api.md"
/// end
///
/// -- List all '.aip' files in a specific pack (absolute paths, no detailed meta)
/// local agent_files = aip.file.list("**/*.aip", {
///   base_dir = "ns@pack/",
///   absolute = true,
///   with_meta = false
/// })
/// for _, file in ipairs(agent_files) do
///   print(file.path) -- e.g., "/path/to/workspace/.aipack/ns/pack/agent1.aip"
/// end
///
/// -- List text and config files from the workspace root
/// local config_files = aip.file.list({"*.txt", "*.config"})
/// for _, file in ipairs(config_files) do
///   print(file.path, file.size) -- e.g., "notes.txt", 1024
/// end
/// ```
///
/// ### Error
///
/// Returns an error if:
/// - `include_globs` is not a string or a list of strings.
/// - `base_dir` cannot be resolved (e.g., invalid pack reference).
/// - An error occurs during file system traversal or glob matching.
/// - Metadata cannot be retrieved (and `with_meta` is true).
///
/// ```ts
/// {
///   error: string // Error message
/// }
/// ```
pub(super) fn file_list(
	lua: &Lua,
	runtime: &Runtime,
	include_globs: Value,
	options: Option<Value>,
) -> mlua::Result<Value> {
	let (base_path, include_globs) = base_dir_and_globs(runtime, include_globs, options.as_ref())?;
	let absolute = options.x_get_bool("absolute").unwrap_or(false);

	// NOTE: For now, not `with_meta` flag always true. Might add it to `list_files_with_options` later.
	// Default is true, as we want convenient APIs, and offer user way to optimize it
	// let with_meta = options.x_get_bool("with_meta").unwrap_or(true);

	let spaths = list_files_with_options(runtime, base_path.as_ref(), &include_globs.x_as_strs(), absolute, true)?;

	let file_infos: Vec<FileInfo> = spaths
		.into_iter()
		.map(|f_ref| FileInfo::from_file_ref(runtime.dir_context(), f_ref))
		.collect();

	let res = file_infos.into_lua(lua)?;

	Ok(res)
}

/// ## Lua Documentation
///
/// Lists and loads files ([`FileRecord`]) matching glob patterns.
///
/// ```lua
/// -- API Signature
/// aip.file.list_load(
///   include_globs: string | list<string>,
///   options?: {
///     base_dir?: string,
///     absolute?: boolean
///   }
/// ): list<FileRecord>
/// ```
///
/// Finds files matching the `include_globs` patterns within the specified `base_dir` (or workspace root),
/// loads the content of each matching file, and returns a list of `FileRecord` objects.
/// Each `FileRecord` contains both metadata and the file content.
///
/// ### Arguments
///
/// - `include_globs: string | list<string>` - A single glob pattern string or a Lua list (table) of glob pattern strings.
///   Globs can include standard wildcards (`*`, `?`, `**`, `[]`). Pack references (e.g., `ns@pack/**/*.md`) are supported.
///   Note: Common build/dependency folders (e.g., `target/`, `node_modules/`, `.build/`, `__pycache__/`)
///   are excluded by default unless explicitly matched by the globs.
///
/// - `options?: table` (optional) - A table containing options:
///   - `base_dir?: string` (optional): The directory relative to which the `include_globs` are applied.
///     Defaults to the workspace root. Pack references (e.g., `ns@pack/`) are supported.
///   - `absolute?: boolean` (optional): If `true`, the paths used internally and potentially the `path` in the returned `FileRecord`
///     objects will be absolute. If `false` (default), paths will generally be relative to the `base_dir`.
///     Note: The exact path stored in `FileRecord.path` depends on internal resolution logic, especially if paths resolve outside `base_dir`.
///
/// ### Returns
///
/// - `list<FileRecord>`: A list of [`FileRecord`] objects, each with content.
///   Returns an empty list if no files match.
///
/// ### Example
///
/// ```lua
/// -- Load all Markdown files in the 'docs' directory
/// local doc_files = aip.file.list_load("*.md", { base_dir = "docs" })
/// for _, file in ipairs(doc_files) do
///   print("--- File:", file.path, "---")
///   print(file.content)
/// end
///
/// -- Load all '.aip' files in a specific pack
/// local agent_files = aip.file.list_load("**/*.aip", { base_dir = "ns@pack/" })
/// for _, file in ipairs(agent_files) do
///   print("Agent Name:", file.stem)
/// end
/// ```
///
/// ### Error
///
/// Returns an error if:
/// - `include_globs` is not a string or a list of strings.
/// - `base_dir` cannot be resolved (e.g., invalid pack reference).
/// - An error occurs during file system traversal or glob matching.
/// - Any matching file cannot be read or its metadata retrieved.
///
/// ```ts
/// {
///   error: string // Error message
/// }
/// ```
pub(super) fn file_list_load(
	lua: &Lua,
	runtime: &Runtime,
	include_globs: Value,
	options: Option<Value>,
) -> mlua::Result<Value> {
	let (base_path, include_globs) = base_dir_and_globs(runtime, include_globs, options.as_ref())?;

	let absolute = options.x_get_bool("absolute").unwrap_or(false);

	let file_refs = list_files_with_options(runtime, base_path.as_ref(), &include_globs.x_as_strs(), absolute, true)?;

	let file_records = create_file_records(runtime, file_refs, base_path.as_ref(), absolute)?;

	let res = file_records.into_lua(lua)?;

	Ok(res)
}

/// ## Lua Documentation
///
/// Finds the first file matching glob patterns and returns its [`FileInfo`].
///
/// ```lua
/// -- API Signature
/// aip.file.first(
///   include_globs: string | list<string>,
///   options?: {
///     base_dir?: string,
///     absolute?: boolean
///   }
/// ): FileInfo | nil
/// ```
///
/// Searches for files matching the `include_globs` patterns within the specified `base_dir` (or workspace root).
/// It stops searching as soon as the first matching file is found and returns its `FileInfo` object (metadata only, no content).
/// If no matching file is found, it returns `nil`.
///
/// ### Arguments
///
/// - `include_globs: string | list<string>` - A single glob pattern string or a Lua list (table) of glob pattern strings.
///   Globs can include standard wildcards (`*`, `?`, `**`, `[]`). Pack references (e.g., `ns@pack/**/*.md`) are supported.
///   Note: Common build/dependency folders (e.g., `target/`, `node_modules/`, `.build/`, `__pycache__/`)
///   are excluded by default unless explicitly matched by the globs.
///
/// - `options?: table` (optional) - A table containing options:
///   - `base_dir?: string` (optional): The directory relative to which the `include_globs` are applied.
///     Defaults to the workspace root. Pack references (e.g., `ns@pack/`) are supported.
///   - `absolute?: boolean` (optional): If `true`, the `path` in the returned `FileInfo` object (if found) will be absolute.
///     If `false` (default), the `path` will be relative to the `base_dir`. Similar to `aip.file.list`, paths outside `base_dir` become absolute.
///
/// ### Returns
///
/// - `FileInfo | nil`: A [`FileInfo`] object for the first matching file, or `nil` if no match is found.
///
/// ### Example
///
/// ```lua
/// -- Find the first '.aip' file in a pack
/// local agent_meta = aip.file.first("*.aip", { base_dir = "ns@pack/" })
/// if agent_meta then
///   print("Found agent:", agent_meta.path)
///   -- To load its content:
///   -- local agent_file = aip.file.load(agent_meta.path, { base_dir = "ns@pack/" })
///   -- print(agent_file.content)
/// else
///   print("No agent file found in pack.")
/// end
///
/// -- Find any config file in the root
/// local config_meta = aip.file.first({"*.toml", "*.yaml", "*.json"}, { base_dir = "." })
/// if config_meta then
///   print("Config file:", config_meta.name)
/// end
/// ```
///
/// ### Error
///
/// Returns an error if:
/// - `include_globs` is not a string or a list of strings.
/// - `base_dir` cannot be resolved (e.g., invalid pack reference).
/// - An error occurs during file system traversal or glob matching *before* the first file is found.
/// - Metadata cannot be retrieved for the first found file.
///
/// ```ts
/// {
///   error: string // Error message
/// }
/// ```
pub(super) fn file_first(
	lua: &Lua,
	runtime: &Runtime,
	include_globs: Value,
	options: Option<Value>,
) -> mlua::Result<Value> {
	let (base_path, include_globs) = base_dir_and_globs(runtime, include_globs, options.as_ref())?;

	let absolute = options.x_get_bool("absolute").unwrap_or(false);

	let base_path = match base_path {
		Some(base_path) => base_path.clone(),
		None => runtime
			.dir_context()
			.wks_dir()
			.ok_or(crate::Error::custom("Cannot create file records, no workspace"))?
			.clone(),
	};

	let mut sfiles = iter_files(
		&base_path,
		Some(&include_globs.iter().map(|s| s.as_str()).collect::<Vec<&str>>()),
		Some(simple_fs::ListOptions::from_relative_glob(true)),
	)
	.map_err(crate::Error::from)?;

	let Some(sfile) = sfiles.next() else {
		return Ok(Value::Nil);
	};

	let absolute_path = SPath::from(&sfile);

	let spath = if absolute {
		sfile
	} else {
		sfile
			.try_diff(&base_path)
			.map_err(|err| crate::Error::cc("Cannot diff with base_path", err))?
	};

	let res = FileInfo::new(runtime.dir_context(), spath, &absolute_path).into_lua(lua)?;

	Ok(res)
}

// region:    --- Tests

#[cfg(test)]
mod tests {
	type Result<T> = core::result::Result<T, Box<dyn std::error::Error>>; // For tests.

	use crate::_test_support::{assert_contains, eval_lua, run_reflective_agent, setup_lua};
	use crate::script::aip_modules::aip_file;
	use serde_json::Value;
	use simple_fs::SPath;
	use std::collections::HashMap;
	use value_ext::JsonValueExt as _;

	#[tokio::test]
	async fn test_lua_file_load_simple_ok() -> Result<()> {
		// -- Setup & Fixtures
		let fx_path = "./agent-script/agent-hello.aip";

		// -- Exec
		let res = run_reflective_agent(&format!(r#"return aip.file.load("{fx_path}")"#), None).await?;

		// -- Check
		assert_contains(res.x_get_str("content")?, "from agent-hello.aip");
		assert_eq!(res.x_get_str("path")?, fx_path);
		assert_eq!(res.x_get_str("name")?, "agent-hello.aip");

		Ok(())
	}

	#[tokio::test]
	async fn test_lua_file_load_pack_ref_simple() -> Result<()> {
		// -- Setup & Fixtures
		let fx_path = "ns_b@pack_b_2/main.aip";

		// -- Exec
		let res = run_reflective_agent(&format!(r#"return aip.file.load("{fx_path}")"#), None).await?;

		// -- Check
		assert_contains(res.x_get_str("content")?, "custom ns_b@pack_b_2 main.aip");
		assert_contains(res.x_get_str("path")?, "pack_b_2/main.aip");
		assert_eq!(res.x_get_str("name")?, "main.aip");

		Ok(())
	}

	#[tokio::test]
	async fn test_lua_file_load_pack_ref_base_support() -> Result<()> {
		// -- Setup & Fixtures
		let fx_path = "ns_b@pack_b_2$base/extra/test.txt";

		// -- Exec
		let res = run_reflective_agent(&format!(r#"return aip.file.load("{fx_path}")"#), None).await?;

		// -- Check
		assert_contains(
			res.x_get_str("content")?,
			"Some support content - ..@..$base/extra/test.txt",
		);
		// NOTE: right now it gives back the path given (the `ns_b@pack_b_2$base/...`)
		// Will be resolve that this: ".aipack-base/support/pack/ns_b/pack_b_2/extra/test.txt",
		assert_contains(res.x_get_str("path")?, fx_path);

		assert_eq!(res.x_get_str("name")?, "test.txt");

		Ok(())
	}

	#[tokio::test]
	async fn test_lua_file_load_pack_ref_workspace_support() -> Result<()> {
		// -- Setup & Fixtures
		let fx_path = "ns_a@pack_a_1$workspace/extra/test.txt";

		// -- Exec
		let res = run_reflective_agent(&format!(r#"return aip.file.load("{fx_path}")"#), None).await?;

		// -- Check
		assert_contains(
			res.x_get_str("content")?,
			"Some support content - ..@..$workspace/extra/test.txt",
		);
		// NOTE: right now it gives back the path given (the `ns_a@pack_a_1$workspace/...`)
		// ".aipack/support/pack/ns_a/pack_a_1/extra/test.txt",
		assert_contains(res.x_get_str("path")?, fx_path);
		assert_eq!(res.x_get_str("name")?, "test.txt");

		Ok(())
	}

	#[tokio::test]
	async fn test_lua_file_exists_true() -> Result<()> {
		// -- Setup & Fixtures
		let lua = setup_lua(aip_file::init_module, "file").await?;
		let paths = &[
			"./agent-script/agent-hello.aip",
			"agent-script/agent-hello.aip",
			"./sub-dir-a/agent-hello-2.aip",
			"sub-dir-a/agent-hello-2.aip",
			"./sub-dir-a/",
			"sub-dir-a",
			"./sub-dir-a/",
			"./sub-dir-a/../",
			"./sub-dir-a/..",
			// Pack references
			"ns_b@pack_b_2/main.aip",
			"ns_a@pack_a_1$workspace/extra/test.txt",
		];

		// -- Exec & Check
		for path in paths {
			let code = format!(r#"return aip.file.exists("{path}")"#);
			let res = eval_lua(&lua, &code)?;
			assert!(res.as_bool().ok_or("Result should be a bool")?, "Should exist: {path}");
		}

		Ok(())
	}

	#[tokio::test]
	async fn test_lua_file_info_ok() -> Result<()> {
		// -- Exec
		let fx_path = "./agent-script/agent-hello.aip";
		let res = run_reflective_agent(&format!(r#"return aip.file.info("{fx_path}")"#), None).await?;

		// -- Check
		assert_eq!(res.x_get_str("name")?, "agent-hello.aip");
		assert_eq!(res.x_get_str("path")?, fx_path);

		Ok(())
	}

	#[tokio::test]
	async fn test_lua_file_info_not_found() -> Result<()> {
		// -- Exec
		let res = run_reflective_agent(r#"return aip.file.info("not/a/file.txt")"#, None).await?;

		// -- Check
		assert_eq!(res, serde_json::Value::Null, "Should have returned null");

		Ok(())
	}

	#[tokio::test]
	async fn test_lua_file_exists_false() -> Result<()> {
		// -- Setup & Fixtures
		let lua = setup_lua(aip_file::init_module, "file").await?;
		let paths = &[
			"./no file .rs",
			"some/no-file.md",
			"./s do/",
			"no-dir/at/all",
			"non_existent_ns@non_existent_pack/file.txt",
		];

		// -- Exec & Check
		for path in paths {
			let code = format!(r#"return aip.file.exists("{path}")"#);
			let res = eval_lua(&lua, &code);

			let res = res?;
			assert!(
				!res.as_bool().ok_or("Result should be a bool")?,
				"Should NOT exist: {path}"
			);
		}
		Ok(())
	}

	#[tokio::test]
	async fn test_lua_file_list_glob_direct() -> Result<()> {
		// -- Fixtures
		// This is the rust Path logic
		let glob = "*.*";

		// -- Exec
		let res = run_reflective_agent(&format!(r#"return aip.file.list("{glob}");"#), None).await?;

		// -- Check
		let res_paths = to_res_paths(&res)?;
		assert_eq!(res_paths.len(), 2, "result length");
		assert_contains(&res_paths, "file-01.txt");
		assert_contains(&res_paths, "file-02.txt");

		Ok(())
	}

	#[tokio::test]
	async fn test_lua_file_list_support_workspace() -> Result<()> {
		// -- Fixtures
		// This is the rust Path logic
		let glob = "ns_b@pack_b_2$base/**/*.*";

		// -- Exec
		let res = run_reflective_agent(&format!(r#"return aip.file.list("{glob}");"#), None).await?;

		// -- Check
		let res = res.as_array().ok_or("Should return an array")?;
		assert_eq!(res.len(), 1, "result length");
		let item = res.first().ok_or("Should have one item")?;
		assert_contains(item.x_get_str("path")?, "extra/test.txt");

		Ok(())
	}

	#[tokio::test]
	async fn test_lua_file_list_glob_deep() -> Result<()> {
		// -- Fixtures
		// This is the rust Path logic
		let glob = "sub-dir-a/**/*.*";

		// -- Exec
		let res = run_reflective_agent(&format!(r#"return aip.file.list("{glob}");"#), None).await?;

		// -- Check
		let res_paths = to_res_paths(&res)?;
		assert_eq!(res_paths.len(), 3, "result length");
		assert_contains(&res_paths, "sub-dir-a/sub-sub-dir/agent-hello-3.aip");
		assert_contains(&res_paths, "sub-dir-a/sub-sub-dir/main.aip");
		assert_contains(&res_paths, "sub-dir-a/agent-hello-2.aip");

		Ok(())
	}

	#[tokio::test]
	async fn test_lua_file_list_negative_glob_absolute() -> Result<()> {
		// -- Exec
		let res = run_reflective_agent(
			r#"return aip.file.list({"**/*.aip", "!sub-dir-a/**/*.aip"}, { absolute = true })"#,
			None,
		)
		.await?;

		// -- Check
		let res_paths = to_res_paths(&res)?;
		assert!(!res_paths.is_empty(), "Should have at least one aip file");
		assert_contains(&res_paths, "agent-script/agent-hello.aip");
		for path in res_paths {
			assert!(
				!path.contains("sub-dir-a/"),
				"Negative glob should exclude sub-dir-a paths even when absolute = true. Got: {path}"
			);
		}

		Ok(())
	}

	#[tokio::test]
	async fn test_lua_file_first_negative_glob_absolute() -> Result<()> {
		// -- Exec
		let res = run_reflective_agent(
			r#"return aip.file.first({"sub-dir-a/**/*.*", "!sub-dir-a/agent-hello-2.aip", "!sub-dir-a/sub-sub-dir/agent-hello-3.aip"}, { absolute = true })"#,
			None,
		)
		.await?;

		// -- Check
		assert_eq!(res.x_get_str("name")?, "main.aip");
		assert_contains(res.x_get_str("path")?, "sub-dir-a/sub-sub-dir/main.aip");

		Ok(())
	}

	#[tokio::test]
	async fn test_lua_file_list_glob_abs_with_wild() -> Result<()> {
		// -- Fixtures
		let lua = setup_lua(aip_file::init_module, "file").await?;
		let dir = SPath::new("./tests-data/config");
		let dir = dir
			.canonicalize()
			.map_err(|err| format!("Cannot canonicalize {dir:?} cause: {err}"))?;

		// This is the rust Path logic
		let glob = format!("{dir}/*.*");
		let code = format!(r#"return aip.file.list("{glob}");"#);

		// -- Exec
		let res = eval_lua(&lua, &code)?;

		// -- Check
		let res = res.as_array().ok_or("Should be array")?;

		assert_eq!(res.len(), 1);
		let val = res.first().ok_or("Should have one item")?;
		assert_eq!(val.x_get_str("name")?, "config-current-with-aliases.toml");

		Ok(())
	}

	#[tokio::test]
	async fn test_lua_file_list_glob_with_base_dir_all_nested() -> Result<()> {
		// -- Setup & Fixtures
		let lua = setup_lua(super::super::init_module, "file").await?;
		let lua_code = r#"
local files = aip.file.list({"**/*.*"}, {base_dir = "sub-dir-a"})
return { files = files }
        "#;

		// -- Exec
		let res = eval_lua(&lua, lua_code)?;

		// -- Check
		let files = res
			.get("files")
			.ok_or("Should have .files")?
			.as_array()
			.ok_or("file should be array")?;

		assert_eq!(files.len(), 3, ".files.len() should be 3");

		// Just create a map fby name
		let file_by_name: HashMap<&str, &Value> =
			files.iter().map(|v| (v.x_get_str("name").unwrap_or_default(), v)).collect();

		let file = file_by_name.get("main.aip").ok_or("Should have 'main.aip'")?;
		assert_eq!(file.x_get_str("path")?, "sub-sub-dir/main.aip");
		assert!(file.x_get_i64("size")? >= 0, "Should have size >= 0");
		let file = file_by_name.get("agent-hello-3.aip").ok_or("Should have 'agent-hello-3.aip'")?;
		assert_eq!(file.x_get_str("path")?, "sub-sub-dir/agent-hello-3.aip");
		assert!(file.x_get_i64("size")? >= 0, "Should have size >= 0");
		let file = file_by_name.get("agent-hello-2.aip").ok_or("Should have 'agent-hello-2.aip'")?;
		assert_eq!(file.x_get_str("path")?, "agent-hello-2.aip");
		assert!(file.x_get_i64("size")? >= 0, "Should have size >= 0");

		Ok(())
	}

	#[tokio::test]
	async fn test_lua_file_list_glob_with_base_dir_one_level() -> Result<()> {
		// -- Setup & Fixtures
		let lua = setup_lua(super::super::init_module, "file").await?;
		let lua_code = r#"
local files = aip.file.list({"agent-hello-*.aip"}, {base_dir = "sub-dir-a"})
return { files = files }
        "#;

		// -- Exec
		let res = eval_lua(&lua, lua_code)?;

		// -- Check
		let files = res
			.get("files")
			.ok_or("Should have .files")?
			.as_array()
			.ok_or("file should be array")?;

		assert_eq!(files.len(), 1, ".files.len() should be 1");
		// NOTE: Here we assume the order will be deterministic and the same across OSes (tested on Mac).
		//       This logic might need to be changed, or actually, the list might need to have a fixed order.
		assert_eq!(
			"agent-hello-2.aip",
			files.first().ok_or("Should have a least one file")?.x_get_str("name")?
		);

		Ok(())
	}

	#[tokio::test]
	async fn test_lua_file_first_glob_deep() -> Result<()> {
		// -- Fixtures
		// This is the rust Path logic
		let glob = "sub-dir-a/**/*-2.*";

		// -- Exec
		let res = run_reflective_agent(&format!(r#"return aip.file.first("{glob}");"#), None).await?;

		// -- Check
		// let res_paths = to_res_paths(&res);
		assert_eq!(res.x_get_str("name")?, "agent-hello-2.aip");
		assert_eq!(res.x_get_str("path")?, "sub-dir-a/agent-hello-2.aip");

		Ok(())
	}

	#[tokio::test]
	async fn test_lua_file_first_not_found() -> Result<()> {
		// -- Fixtures
		// This is the rust Path logic
		let glob = "sub-dir-a/**/*-not-a-thing.*";

		// -- Exec
		let res = run_reflective_agent(&format!(r#"return aip.file.first("{glob}")"#), None).await?;

		// -- Check
		assert_eq!(res, serde_json::Value::Null, "Should have returned null");

		Ok(())
	}

	// region:    --- Support for Tests

	fn to_res_paths(res: &serde_json::Value) -> Result<Vec<&str>> {
		let arr = res.as_array().ok_or("should have array of path")?;

		let v = arr
			.iter()
			.map(|v| v.x_get_as::<&str>("path").unwrap_or_default())
			.collect::<Vec<&str>>();

		Ok(v)
	}

	// endregion: --- Support for Tests
}

// endregion: --- Tests