import { NgFor, NgIf } from '@angular/common';
import { Component, Input, OnInit } from '@angular/core';
import { FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
import { api } from '@enums/api.enum';
import { constants } from '@enums/constants.enum';
import { ReportForm } from '@interfaces/form';
import { IonicModule, ModalController } from '@ionic/angular';
import { AlertService } from '@services/alert.service';
import { HttpRequestService } from '@services/http-request.service';

@Component({
  selector: 'app-report-content',
  templateUrl: './report-content.component.html',
  styleUrls: ['./report-content.component.scss'],
  standalone: true,
  imports: [IonicModule, FormsModule, ReactiveFormsModule, NgFor, NgIf]
})
export class ReportContentComponent implements OnInit {
  @Input() data: any;
  reportSubjects = constants.reportSubjects;
  pType = 'shravak';
  reportForm: FormGroup<ReportForm>;

  constructor(
    private httpRequestService: HttpRequestService,
    private alert: AlertService,
    private modalController: ModalController
  ) { }

  ngOnInit() {
    this.pType = this.data.type;
    this.reportForm = new FormGroup<ReportForm>({
      type: new FormControl(this.data.postId ? 2 : this.data.commentId ? 3 : 1),
      reported_for: new FormControl(
        this.data.postId ? this.data.postId :
          this.data.commentId ? this.data.commentId : this.data.userId
      ),
      subject: new FormControl(null, Validators.required),
      description: new FormControl(null)
    });
  }

  submit() {
    this.reportForm.markAsDirty();
    if (this.reportForm.invalid) {
      return;
    }
    this.alert.showLoader();
    this.httpRequestService.post(api.flagReport, this.reportForm.value)
      .then((res: any) => {
        this.alert.showToaster(res.message);
      }).finally(() => {
        this.modalController.dismiss();
        this.alert.hideLoader();
      });
  }

}
