aboutsummaryrefslogtreecommitdiffstats
path: root/source/app/Http/Controllers/MatchController.php
blob: cbea61bb21aaed6a3b305227040efae764568987 (plain) (blame)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class MatchController extends Controller
{
    /**
     * Get match percentage between a student and a company
     *
     * @param string $student_id
     * @param string $company_id
     * @return JsonResponse
     */
    public function getMatchPercentage(string $student_id, string $company_id): JsonResponse
    {
        try {
            $response = Http::get("{$this->apiUrl}match/{$student_id}/{$company_id}");

            if ($response->successful()) {
                $data = $response->json();

                // Log successful match fetch for debugging
                Log::info("Match percentage fetched successfully", [
                    'student_id' => $student_id,
                    'company_id' => $company_id,
                    'match_percentage' => $data['match_percentage'] ?? 0
                ]);

                return response()->json($data);
            } else {
                Log::warning("Match API returned non-successful response", [
                    'student_id' => $student_id,
                    'company_id' => $company_id,
                    'status' => $response->status(),
                    'body' => $response->body()
                ]);

                return response()->json(['match_percentage' => 0], 200);
            }
        } catch (\Exception $e) {
            Log::error("Error fetching match percentage", [
                'student_id' => $student_id,
                'company_id' => $company_id,
                'error' => $e->getMessage()
            ]);

            return response()->json(['match_percentage' => 0], 200);
        }
    }

    /**
     * Get match percentages for multiple companies for a specific student
     *
     * @param Request $request
     * @param string $student_id
     * @return JsonResponse
     */
    public function getMultipleMatches(Request $request, string $student_id): JsonResponse
    {
        $request->validate([
            'company_ids' => 'required|array',
            'company_ids.*' => 'required|integer'
        ]);

        $company_ids = $request->input('company_ids');
        $matches = [];

        foreach ($company_ids as $company_id) {
            try {
                $response = Http::get("{$this->apiUrl}match/{$student_id}/{$company_id}");

                if ($response->successful()) {
                    $data = $response->json();
                    $matches[$company_id] = $data['match_percentage'] ?? 0;
                } else {
                    $matches[$company_id] = 0;
                }
            } catch (\Exception $e) {
                Log::error("Error fetching match for company {$company_id}", [
                    'student_id' => $student_id,
                    'company_id' => $company_id,
                    'error' => $e->getMessage()
                ]);
                $matches[$company_id] = 0;
            }
        }

        return response()->json(['matches' => $matches]);
    }

    /**
     * Get companies sorted by match percentage for a student
     *
     * @param string $student_id
     * @return JsonResponse
     */
    public function getSortedCompaniesByMatch(string $student_id): JsonResponse
    {
        try {
            // First get all companies
            $companiesResponse = Http::get("{$this->companiesApiUrl}");

            if (!$companiesResponse->successful()) {
                return response()->json(['error' => 'Failed to fetch companies'], 500);
            }

            $companies = $companiesResponse->json('data');
            $companiesWithMatches = [];

            // Fetch match percentage for each company
            foreach ($companies as $company) {
                try {
                    $matchResponse = Http::get("{$this->apiUrl}match/{$student_id}/{$company['id']}");

                    if ($matchResponse->successful()) {
                        $matchData = $matchResponse->json();
                        $company['match_percentage'] = $matchData['match_percentage'] ?? 0;
                    } else {
                        $company['match_percentage'] = 0;
                    }
                } catch (\Exception $e) {
                    $company['match_percentage'] = 0;
                }

                $companiesWithMatches[] = $company;
            }

            // Sort by match percentage (highest first)
            usort($companiesWithMatches, function($a, $b) {
                return ($b['match_percentage'] ?? 0) <=> ($a['match_percentage'] ?? 0);
            });

            return response()->json(['companies' => $companiesWithMatches]);

        } catch (\Exception $e) {
            Log::error("Error getting sorted companies by match", [
                'student_id' => $student_id,
                'error' => $e->getMessage()
            ]);

            return response()->json(['error' => 'Internal server error'], 500);
        }
    }

    /**
     * Get companies that a student has liked (for matches/chat)
     *
     * @param string $student_id
     * @return JsonResponse
     */
    public function getLikedCompaniesByStudent(string $student_id): JsonResponse
    {
        try {
            // Get all connections for this student
            $connectionsResponse = Http::get("{$this->connectionsApiUrl}");

            if (!$connectionsResponse->successful()) {
                return response()->json(['error' => 'Failed to fetch connections'], 500);
            }

            $allConnections = $connectionsResponse->json('data');

            // Filter to only liked connections (status = true) for this student
            $likedConnections = collect($allConnections)
                ->where('student_id', $student_id)
                ->where('status', true)
                ->all();

            if (empty($likedConnections)) {
                return response()->json(['companies' => []]);
            }

            // Get company IDs that were liked
            $likedCompanyIds = collect($likedConnections)->pluck('company_id')->toArray();

            // Fetch all companies
            $companiesResponse = Http::get("{$this->companiesApiUrl}");

            if (!$companiesResponse->successful()) {
                return response()->json(['error' => 'Failed to fetch companies'], 500);
            }

            $allCompanies = $companiesResponse->json('data');

            // Filter companies to only include liked ones
            $likedCompanies = collect($allCompanies)
                ->whereIn('id', $likedCompanyIds)
                ->values()
                ->toArray();

            return response()->json(['companies' => $likedCompanies]);

        } catch (\Exception $e) {
            Log::error("Error getting liked companies by student", [
                'student_id' => $student_id,
                'error' => $e->getMessage()
            ]);

            return response()->json(['error' => 'Internal server error'], 500);
        }
    }
}