<?php

namespace App\Admin\Repositories;

use App\Models\DistAppearanceVariable as Model;
use Dcat\Admin\Form;
use Dcat\Admin\Repositories\EloquentRepository;
use App\Models\SiteAppearanceVariable;
use Illuminate\Support\Carbon;

class DistAppearanceVariable extends EloquentRepository
{
    /**
     * Model.
     *
     * @var string
     */
    protected $eloquentClass = Model::class;


    public static function getVariableRow($distId,$appearanceId,$templateId)
    {
        $model = new Model();
        $variable = $model->where('dist_id',$distId)->where('appearance_id',$appearanceId)->whereIn('template_id',[0,$templateId])->get();
        if($variable){
            return $variable;
        }else{
            return '';
        }
    }

    /*
     * 把原始变量复制给分销商
     */
    public static function copyAppearanceVariable($appearanceId, $distId){
        Model::copyAppearanceVariable($appearanceId, $distId);
    }


    /*
     * 删除分销商主题变量
     */
    public static function deleteVariable($appearanceId,$distId)
    {
        return Model::deleteVariable($appearanceId, $distId);
    }


    /*
     * 同步变量到正式表
     */
    public static function syncAppearanceVariables($appearanceId,$distId) {
        $appearanceId = intval($appearanceId);
        $distId = intval($distId);

        // Define criteria for filtering
        $criteria = ['dist_id' => $distId, 'appearance_id' => $appearanceId];

        $tmpModel = new Model();
        // Retrieve records from both tables
        $tempVariables = $tmpModel
            ->where($criteria)
            ->get();

        $siteModel = new SiteAppearanceVariable();
        $siteVariables = $siteModel
            ->where($criteria)
            ->get()
            ->keyBy('id'); // Use IDs as keys for easier comparison

        foreach ($tempVariables as $tempVariable) {
            $siteVariable = $siteVariables->get($tempVariable->id);

            if ($siteVariable) {
                // If record exists in `site_appearance_variable`, check for updates
                if ($tempVariable->updated_at > $siteVariable->updated_at) {
                    $siteModel
                        ->where('id', $siteVariable->id)
                        ->update([
                            'variable_name' => $tempVariable->variable_name,
                            'variable_value' => $tempVariable->variable_value,
                            'variable_type' => $tempVariable->variable_type,
                            'template_code' => $tempVariable->template_code,
                            'variable_code' => $tempVariable->variable_code,
                            'updated_at' => Carbon::now(),
                        ]);
                }
            } else {
                // If record does not exist, insert it
                $siteModel->insert([
                    'id' => $tempVariable->id,
                    'dist_id' => $tempVariable->dist_id,
                    'appearance_id' => $tempVariable->appearance_id,
                    'variable_name' => $tempVariable->variable_name,
                    'variable_value' => $tempVariable->variable_value,
                    'variable_type' => $tempVariable->variable_type,
                    'template_code' => $tempVariable->template_code,
                    'variable_code' => $tempVariable->variable_code,
                    'created_at' => Carbon::now(),
                    'updated_at' => Carbon::now(),
                ]);
            }
        }

        // Delete records from `site_appearance_variable` that don’t match `dist_id=1` and `appearance_id=1`
        $siteModel
            ->where($criteria)
            ->whereNotIn('id', $tempVariables->pluck('id')->toArray())
            ->delete();

    }

}
