import { Component, OnInit } from '@angular/core';
import { BaseService } from 'src/app/shared/service/base.service';


@Component({
  selector: 'app-rules',
  templateUrl: './rules.component.html',
  styleUrls: ['./rules.component.scss']
})
export class RulesComponent implements OnInit {

  /* ================= DATA ================= */

  rules: any[] = [];

  fields: any[] = [];
  operators: any[] = [];
  actionsList: any[] = [];

  /* ================= MODAL ================= */

  showRuleModal = false;
  editMode = false;

ruleForm: any = {
  name: '',
  type: '',
  description: '',
  conditions: [],
  actions: [],
  apiKeys: []   // 🔥 ADD THIS
};

  selectedIndex: number = -1;
apiList: any[] = [];
  constructor(private baseService: BaseService) {}

  ngOnInit() {
    this.loadRules();
    this.loadApis();
    this.loadDropdowns();
  }

  /* ================= API ================= */

  loadRules() {
    this.baseService.getData('rules').subscribe((res: any) => {
      this.rules = res?.data || [];
    });
  }
  loadApis() {
  this.baseService.getData('rules/apis-key').subscribe((res: any) => {
    this.apiList = res?.data || [];
  });
}

  loadDropdowns() {
    this.baseService.getData('rules/dropdowns').subscribe((res: any) => {
      const d = res?.data || {};

      this.fields = d.fields || [];
      this.operators = d.operators || [];
      this.actionsList = d.actions || [];
    });
  }

  /* ================= MODAL ================= */

 openAddRule() {
  this.editMode = false;
  this.ruleForm = {
    name: '',
    type: '',
    description: '',
    conditions: [],
    actions: [],
    apiKeys: []   // 🔥 ADD
  };
  this.showRuleModal = true;
}

  editRule(rule: any, index: number) {
    this.editMode = true;
    this.selectedIndex = index;
    this.ruleForm = JSON.parse(JSON.stringify(rule));
    this.showRuleModal = true;
  }

  deleteRule(rule: any) {
    this.baseService.deleteData(`rules/${rule.id}`).subscribe(() => {
      this.loadRules();
    });
  }

  /* ================= CONDITIONS ================= */

  addCondition() {
    this.ruleForm.conditions.push({
      fieldKey: '',
      operator: '',
      value: ''
    });
  }

  removeCondition(i: number) {
    this.ruleForm.conditions.splice(i, 1);
  }

  /* ================= ACTIONS ================= */

  addAction() {
    this.ruleForm.actions.push({
      fieldKey: '',
      actionType: 'SET',
      value: ''
    });
  }

  removeAction(i: number) {
    this.ruleForm.actions.splice(i, 1);
  }

  /* ================= SAVE ================= */

  saveRule() {
     const params = new URLSearchParams();

  params.set('name', this.ruleForm.name);
  params.set('type', this.ruleForm.type);
  params.set('description', this.ruleForm.description);

  
  params.set('conditionText', JSON.stringify(this.ruleForm.conditions));
  params.set('actionText', JSON.stringify(this.ruleForm.actions));
params.set('apiKeysText', this.ruleForm.apiKeys.join(','));

    this.baseService.postData('rules', params).subscribe(() => {
      this.loadRules();
      
      this.showRuleModal = false;
    });
  }
}