File size: 4,691 Bytes
70556b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using Application.Abstractions.Interfaces;
using Application.DTOs.RobotDTOs;
using Application.DTOs.UserDTOs;
using Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using IAuthService = Application.Abstractions.Interfaces.IAuthorizationService;

namespace RobDeliveryAPI.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class AuthController : ControllerBase
    {
        private readonly ITokenService _tokenService;
        private readonly IAuthService _authorizationService;
        private readonly IRobotService _robotService;

        public AuthController(
            ITokenService tokenService,
            IAuthService authorizationService,
            IRobotService robotService)
        {
            _tokenService = tokenService;
            _authorizationService = authorizationService;
            _robotService = robotService;
        }

        /// <summary>
        /// Register a new user. If account already exists — auto-login.
        /// Google registration returns NeedsAdditionalInfo if user is new (redirect to complete-profile).
        /// </summary>
        [AllowAnonymous]
        [HttpPost("register")]
        public async Task<IActionResult> Register(UserRegisterDTO registerData)
        {
            var result = await _authorizationService.RegisterOrLoginAsync(registerData);

            if (result.Status == "Error")
            {
                return BadRequest(result);
            }

            return Ok(result);
        }

        /// <summary>
        /// Login user. Google login auto-registers if user doesn't exist (returns NeedsAdditionalInfo).
        /// </summary>
        [AllowAnonymous]
        [HttpPost("login")]
        public async Task<IActionResult> Login(UserLoginDTO loginData)
        {
            var result = await _authorizationService.LoginOrRegisterAsync(loginData);

            if (result.Status == "Error")
            {
                return BadRequest(result);
            }

            return Ok(result);
        }

        /// <summary>
        /// Complete Google registration with additional info (phone number, map location).
        /// Called after Google auth returns NeedsAdditionalInfo.
        /// </summary>
        [AllowAnonymous]
        [HttpPost("complete-google-registration")]
        public async Task<IActionResult> CompleteGoogleRegistration(CompleteGoogleRegDTO data)
        {
            var result = await _authorizationService.CompleteGoogleRegistrationAsync(data);

            if (result.Status == "Error")
            {
                return BadRequest(result);
            }

            return Ok(result);
        }

        [AllowAnonymous]
        [HttpPost("robot/register")]
        public async Task<IActionResult> RegisterRobot(RobotRegisterDTO registerData)
        {
            // Use RobotService to register robot (follows Clean Architecture)
            var (success, robotId, errorMessage) = await _robotService.RegisterRobotAsync(registerData);

            if (!success)
            {
                return BadRequest(new { error = errorMessage });
            }

            // Generate token for the robot
            var token = await _tokenService.GenerateRobotToken(robotId!.Value);

            // Get robot details for response
            var robot = await _robotService.GetRobotByIdAsync(robotId.Value);

            return Ok(new
            {
                message = "Robot registered successfully",
                robotId = robotId.Value,
                serialNumber = registerData.SerialNumber,
                token
            });
        }

        [AllowAnonymous]
        [HttpPost("robot/login")]
        public async Task<IActionResult> LoginRobot(RobotLoginDTO loginData)
        {
            // Use RobotService to authenticate robot (follows Clean Architecture)
            var (success, robotId, errorMessage) = await _robotService.AuthenticateRobotAsync(loginData);

            if (!success)
            {
                return Unauthorized(new { error = errorMessage });
            }

            // Generate token for the authenticated robot
            var token = await _tokenService.GenerateRobotToken(robotId!.Value);

            // Get robot details for response
            var robot = await _robotService.GetRobotByIdAsync(robotId.Value);

            return Ok(new
            {
                message = "Robot login successful",
                robotId = robotId.Value,
                serialNumber = robot?.SerialNumber,
                robotName = robot?.Name,
                token
            });
        }
    }
}