function make_boxa_variations()
%MAKE_BOXA_VARIATIONS Variation folders for the two Box-A pooling groupings.
%   MAKE_BOXA_VARIATIONS() writes variations/<grouping>_<window>/ -- each with
%   data.csv + analyze.m (a copy of variation_analyze.m) + result.txt, fitting
%   the paper LME (behavior ~ stim + day + stim:day + (1|rat)) on the success
%   COUNT -- for:
%     boxa_a2   B2           vs A2 + Box-A     ("b2 vs a2+a")
%     boxa_b2   B2 + Box-A   vs A2             ("b2+a vs a2")
%   over windows d0_5 (0-5), d6_10 (6-10), d0_10 (0-10). Box-A is the
%   single-animal Electrode-Box-A condition, pooled into the control (boxa_a2)
%   or the treatment (boxa_b2). Also writes variations/boxa_summary.csv with the
%   residual / Satterthwaite / honest random-slope interaction p-values.

thisDir = fileparts(mfilename('fullpath'));
template = fullfile(thisDir, 'variation_analyze.m');
root = fullfile(thisDir, 'variations');
if ~exist(root, 'dir'); mkdir(root); end

Tc = tdcs_load_data();
B2 = 'Electrode-Box-B2'; A2 = 'Electrode-Box-A2'; BOXA = 'Electrode-Box-A';

% grouping name | treatment groups (stim=1) | control groups (stim=0)
groupings = {
    'boxa_a2', {B2},        {A2, BOXA}
    'boxa_b2', {B2, BOXA},  {A2}
    };
windows = {'d0_5', [0 5]; 'd6_10', [6 10]; 'd0_10', [0 10]};

allGroup = cellstr(Tc.group);
rows = {};
for gi = 1:size(groupings, 1)
    gname = groupings{gi, 1};
    inTreat = ismember(allGroup, groupings{gi, 2});
    inCtrl = ismember(allGroup, groupings{gi, 3});
    for wi = 1:size(windows, 1)
        wname = windows{wi, 1}; w = windows{wi, 2};
        sel = (inTreat | inCtrl) & Tc.day >= w(1) & Tc.day <= w(2);
        D = Tc(sel, :);
        stim = double(inTreat(sel));
        Dout = table(string(D.subject), string(D.group), D.day, D.success, ...
            D.total, stim, 'VariableNames', ...
            {'subject', 'group', 'day', 'success', 'total', 'stim'});

        vname = [gname '_' wname];
        folder = fullfile(root, vname);
        if ~exist(folder, 'dir'); mkdir(folder); end
        writetable(Dout, fullfile(folder, 'data.csv'));
        copyfile(template, fullfile(folder, 'analyze.m'));

        r = localRun(fullfile(folder, 'analyze.m'));
        rows(end + 1, :) = {vname, gname, wname, r.nRats, r.nObs, ...
            r.interP, r.interPsatt, r.interPrs, r.interEst, r.stimP}; %#ok<AGROW>
        fprintf('  %-14s N=%d obs=%3d  int p: res=%.3f satt=%.3f rs=%-5s  est=%+.2f\n', ...
            vname, r.nRats, r.nObs, r.interP, r.interPsatt, localNum(r.interPrs), r.interEst);
    end
end

S = cell2table(rows, 'VariableNames', {'variation', 'grouping', 'window', ...
    'nRats', 'nObs', 'interaction_p', 'interaction_p_satt', 'interaction_p_rs', ...
    'interaction_est', 'stim_p'});
writetable(S, fullfile(root, 'boxa_summary.csv'));
fprintf('\nWrote %d folders + variations/boxa_summary.csv\n', size(rows, 1));

end

function r = localRun(scriptPath)
run(scriptPath);
r = VARRESULT; %#ok<NODEF>  (defined by the analyze.m script just run)
end

function s = localNum(x)
if isnan(x); s = 'n/a'; else; s = sprintf('%.3f', x); end
end
