<?php
namespace App;

class Store {
    public static function datasetDir(string $label): string {
        return DATA_DIR . '/' . $label;
    }

    public static function ensureDatasetDirs(string $label): array {
        $base = self::datasetDir($label);

        $dirs = [
            'raw'        => $base . '/raw',
            'excel'      => $base . '/excel',
            'excel_base' => $base . '/excel/base',
            'excel_final'=> $base . '/excel/final',
            'images'     => $base . '/images',
            'csv'        => $base . '/csv',
            'uploads'    => $base . '/uploads',
            'logs'       => $base . '/logs',
            'txt'        => $base . '/txt',
        ];

        foreach ($dirs as $d) {
            if (!is_dir($d)) {
                @mkdir($d, 0775, true);
            }
        }
        return $dirs;
    }

    // Global uploads folder: DATA_DIR/_uploads
    public static function globalUploadsDir(): string {
        return DATA_DIR . '/_uploads';
    }

    public static function ensureGlobalUploadsDir(): string {
        $d = self::globalUploadsDir();
        if (!is_dir($d)) {
            @mkdir($d, 0775, true);
        }
        return $d;
    }

    public static function listDatasets(): array {
        if (!is_dir(DATA_DIR)) return [];
        $out = [];
        foreach (scandir(DATA_DIR) as $d) {
            if ($d === '.' || $d === '..') continue;
            // skip internal folders like _queue, _cache, _uploads
            if (strpos($d, '_') === 0) continue;
            if (is_dir(DATA_DIR . '/' . $d)) $out[] = $d;
        }
        sort($out, SORT_NATURAL);
        return $out;
    }
}
