import { Request, Response } from "express";
import { AbstractController } from "../abstract.controller";
import { SocialNetworkSmssModel } from "../../models/social-network/social-network-sms.model";
import { SocialNetworkSmssMethods } from "../../methods/social-network/social-network-sms.method";

export class SocialNetworkSmssController extends AbstractController {
  methods: SocialNetworkSmssMethods;
  constructor() {
    super(SocialNetworkSmssModel);
    this.methods = new SocialNetworkSmssMethods(SocialNetworkSmssModel);
  }
  async sendMessage(req: Request | any, res: Response) {
    try {
      const matchedData = this.params.matchedData(req);
      this.methods
        .sendMessages(matchedData.list)
        .then((response) => {
          this.model.create();
          this.methods
            .saveDocument({ ...matchedData, response }, req)
            .then((r) => {
              res.send(r);
            })
            .catch((error) => {
              this.params.handleHttpError(res, error);
            });
        })
        .catch((error) => {
          this.params.handleHttpError(res, error);
        });
    } catch (e) {
      console.log(e);
      this.params.handleHttpError(res, e);
    }
  }
  async sendVerification(req: Request | any, res: Response) {
    try {
      const matchedData = this.params.matchedData(req);
      this.methods
        .sendVerification(matchedData)
        .then((response) => {
          res.send(response);
        })
        .catch((error) => {
          this.params.handleHttpError(res, error);
        });
    } catch (e) {
      console.log(e);
      this.params.handleHttpError(res, e);
    }
  }
  async verify(req: Request | any, res: Response) {
    try {
      const matchedData = this.params.matchedData(req);
      res.send({
        verify: this.methods.verify(matchedData.number, matchedData.code),
      });
    } catch (e) {
      console.log(e);
      this.params.handleHttpError(res, e);
    }
  }
}
