#nullable disable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using TwilioSMSReceiver;
using TwilioSMSReceiver.Data;
using TwilioSMSReceiver.Data.Models;

namespace TwilioSMSReceiver.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MSTeamsWebHooksController : ControllerBase
    {
        private readonly SMSDbCtx _context;

        public MSTeamsWebHooksController(SMSDbCtx context, IWebHostEnvironment environment)
        {
            _context = context;
            if (!environment.IsDevelopment())
            {
                throw new InvalidOperationException("Not supported in production mode");
            }
        }

        // GET: api/MSTeamsWebHooks
        [HttpGet]
        public async Task<ActionResult<IEnumerable<MSTeamsWebHook>>> GetMSTeamsWebHooks()
        {
            return await _context.MSTeamsWebHooks.ToListAsync();
        }

        // GET: api/MSTeamsWebHooks/5
        [HttpGet("{id}")]
        public async Task<ActionResult<MSTeamsWebHook>> GetMSTeamsWebHook(int id)
        {
            var mSTeamsWebHook = await _context.MSTeamsWebHooks.FindAsync(id);

            if (mSTeamsWebHook == null)
            {
                return NotFound();
            }

            return mSTeamsWebHook;
        }

        // PUT: api/MSTeamsWebHooks/5
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPut("{id}")]
        public async Task<IActionResult> PutMSTeamsWebHook(int id, MSTeamsWebHook mSTeamsWebHook)
        {
            if (id != mSTeamsWebHook.Id)
            {
                return BadRequest();
            }

            _context.Entry(mSTeamsWebHook).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MSTeamsWebHookExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/MSTeamsWebHooks
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPost]
        public async Task<ActionResult<MSTeamsWebHook>> PostMSTeamsWebHook([FromForm]string teamsHookUrl)
        {
            var mSTeamsWebHook = new MSTeamsWebHook
            {
                WebHookUri = teamsHookUrl
            };
            _context.MSTeamsWebHooks.Add(mSTeamsWebHook);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetMSTeamsWebHook", new { id = mSTeamsWebHook.Id }, mSTeamsWebHook);
        }

        // DELETE: api/MSTeamsWebHooks/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteMSTeamsWebHook(int id)
        {
            var mSTeamsWebHook = await _context.MSTeamsWebHooks.FindAsync(id);
            if (mSTeamsWebHook == null)
            {
                return NotFound();
            }

            _context.MSTeamsWebHooks.Remove(mSTeamsWebHook);
            await _context.SaveChangesAsync();

            return NoContent();
        }

        private bool MSTeamsWebHookExists(int id)
        {
            return _context.MSTeamsWebHooks.Any(e => e.Id == id);
        }
    }
}
