<?php

namespace App\Controllers\Api;

use CodeIgniter\HTTP\ResponseInterface;

class FishingController extends BaseApiController
{
    protected $format = 'json';
    protected $table = 'fishing';

    /**
     * 낚시터 목록
     * GET /api/fishing/list
     */
    public function index()
    {
        $auth = $this->requireAuth();
        if ($auth instanceof ResponseInterface) {
            return $auth;
        }

        try {
            $page = (int) ($this->request->getGet('page') ?? 1);
            $perPage = (int) ($this->request->getGet('per_page') ?? 10);
            if ($page < 1) $page = 1;
            if ($perPage < 1) $perPage = 10;
            $offset = ($page - 1) * $perPage;

            $searchField = trim((string) $this->request->getGet('search_field')); // '', field, area, name, fish_species
            $search      = trim((string) $this->request->getGet('search'));
            $partnership = trim((string) $this->request->getGet('partnership'));
            $status      = trim((string) $this->request->getGet('status'));
            $startDate   = trim((string) $this->request->getGet('start_date'));
            $endDate     = trim((string) $this->request->getGet('end_date'));

            $db = $this->getDB();
            $builder = $db->table($this->table . ' fs');
            $builder->join('fishing_field f', 'f.id = fs.field_id', 'left');
            $builder->join('fishing_area a', 'a.id = fs.area_id', 'left');
            $builder->where('fs.deleted_YN', 'N');

            if ($search !== '') {
                if ($searchField === 'field') {
                    $builder->like('f.name', $search);
                } elseif ($searchField === 'area') {
                    $builder->like('a.name', $search);
                } elseif ($searchField === 'name') {
                    $builder->like('fs.name', $search);
                } elseif ($searchField === 'fish_species') {
                    $builder->like('fs.fish_species', $search);
                } else {
                    // 전체: 분야 / 지역명 / 낚시터명 / 어종
                    $builder->groupStart()
                        ->like('f.name', $search)
                        ->orLike('a.name', $search)
                        ->orLike('fs.name', $search)
                        ->orLike('fs.fish_species', $search)
                        ->groupEnd();
                }
            }
            if ($partnership === 'Y' || $partnership === 'N') {
                $builder->where('fs.partnership_YN', $partnership);
            }
            if ($status === 'Y' || $status === 'N') {
                $builder->where('fs.status_YN', $status);
            }
            if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $startDate)) {
                $builder->where('fs.created_at >=', $startDate . ' 00:00:00');
            }
            if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $endDate)) {
                $builder->where('fs.created_at <=', $endDate . ' 23:59:59');
            }

            $total = $builder->countAllResults(false);

            // 계좌번호는 목록에서 제외 (민감정보)
            $items = $builder
                ->select('fs.id, fs.name, fs.field_id, fs.area_id, fs.operating_hours, fs.fish_species, fs.address, fs.partnership_YN, fs.status_YN, fs.created_at, f.name as field_name, a.name as area_name')
                ->orderBy('fs.id', 'DESC')
                ->limit($perPage, $offset)
                ->get()
                ->getResult();

            return $this->respondSuccess([
                'items'       => $items,
                'total'       => $total,
                'page'        => $page,
                'per_page'    => $perPage,
                'total_pages' => (int) ceil($total / $perPage),
            ]);
        } catch (\Exception $e) {
            log_message('error', 'FishingController index error: ' . $e->getMessage());
            return $this->respondError('목록 조회 중 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 낚시터 등록
     * POST /api/fishing
     */
    public function create()
    {
        $auth = $this->requireAuth();
        if ($auth instanceof ResponseInterface) {
            return $auth;
        }

        try {
            $payload = $this->request->getJSON(true);
            if (!is_array($payload) || empty($payload)) {
                $payload = $this->request->getPost() ?? [];
            }

            $fieldId = (int) ($payload['field_id'] ?? 0);
            $areaId  = (int) ($payload['area_id'] ?? 0);
            $name    = trim((string) ($payload['name'] ?? ''));

            // 필수값 검증
            if ($fieldId <= 0) {
                return $this->respondError('분야를 선택하세요.', ResponseInterface::HTTP_BAD_REQUEST);
            }
            if ($areaId <= 0) {
                return $this->respondError('지역을 선택하세요.', ResponseInterface::HTTP_BAD_REQUEST);
            }
            if ($name === '') {
                return $this->respondError('낚시터명을 입력하세요.', ResponseInterface::HTTP_BAD_REQUEST);
            }
            if (mb_strlen($name) > 100) {
                return $this->respondError('낚시터명은 100자 이내로 입력하세요.', ResponseInterface::HTTP_BAD_REQUEST);
            }

            $db = $this->getDB();

            // 분야 / 지역 존재 확인
            $fieldExists = $db->table('fishing_field')
                ->where('id', $fieldId)->where('deleted_YN', 'N')->countAllResults();
            if ($fieldExists === 0) {
                return $this->respondError('존재하지 않는 분야입니다.', ResponseInterface::HTTP_BAD_REQUEST);
            }

            $areaExists = $db->table('fishing_area')
                ->where('id', $areaId)->where('deleted_YN', 'N')->countAllResults();
            if ($areaExists === 0) {
                return $this->respondError('존재하지 않는 지역입니다.', ResponseInterface::HTTP_BAD_REQUEST);
            }

            $partnership = (($payload['partnership_YN'] ?? 'N') === 'Y') ? 'Y' : 'N';
            $status      = (($payload['status_YN'] ?? 'Y') === 'N') ? 'N' : 'Y';

            $insertData = [
                'field_id'        => $fieldId,
                'area_id'         => $areaId,
                'name'            => $name,
                'operating_hours' => trim((string) ($payload['operating_hours'] ?? '')),
                'fish_species'    => trim((string) ($payload['fish_species'] ?? '')),
                'zip_code'        => trim((string) ($payload['zip_code'] ?? '')),
                'address'         => trim((string) ($payload['address'] ?? '')),
                'address_detail'  => trim((string) ($payload['address_detail'] ?? '')),
                'address_refer'   => trim((string) ($payload['address_refer'] ?? '')),
                'lat'             => trim((string) ($payload['lat'] ?? '')),
                'lng'             => trim((string) ($payload['lng'] ?? '')),
                'partnership_YN'  => $partnership,
                'status_YN'       => $status,
                'created_at'      => date('Y-m-d H:i:s'),
            ];

            // 제휴인 경우에만 계좌 정보 저장 (계좌번호 양방향 암호화)
            if ($partnership === 'Y') {
                $insertData['bank_code']      = trim((string) ($payload['bank_code'] ?? ''));
                $insertData['account_number'] = $this->encryptValue(trim((string) ($payload['account_number'] ?? '')));
                $insertData['account_holder'] = trim((string) ($payload['account_holder'] ?? ''));
            } else {
                $insertData['bank_code']      = '';
                $insertData['account_number'] = '';
                $insertData['account_holder'] = '';
            }

            if (!$db->table($this->table)->insert($insertData)) {
                return $this->respondError('등록에 실패했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
            }

            $newId = $db->insertID();
            $row = $db->table($this->table)->where('id', $newId)->get()->getRow();
            if ($row) {
                $row->account_number = $this->decryptValue($row->account_number);
            }

            return $this->respondSuccess($row, '낚시터가 등록되었습니다.', ResponseInterface::HTTP_CREATED);
        } catch (\Exception $e) {
            log_message('error', 'FishingController create error: ' . $e->getMessage());
            return $this->respondError('등록 중 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 낚시터 상세 조회
     * GET /api/fishing/:id
     */
    public function show($id = null)
    {
        $auth = $this->requireAuth();
        if ($auth instanceof ResponseInterface) {
            return $auth;
        }

        if (empty($id)) {
            return $this->respondError('ID가 필요합니다.', ResponseInterface::HTTP_BAD_REQUEST);
        }

        try {
            $row = $this->getDB()->table($this->table . ' fs')
                ->select('fs.*, f.name as field_name, a.name as area_name')
                ->join('fishing_field f', 'f.id = fs.field_id', 'left')
                ->join('fishing_area a', 'a.id = fs.area_id', 'left')
                ->where('fs.id', (int) $id)
                ->where('fs.deleted_YN', 'N')
                ->get()
                ->getRow();

            if (!$row) {
                return $this->respondError('해당 낚시터를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
            }

            $row->account_number = $this->decryptValue($row->account_number);

            $row->photos = $this->getDB()->table('fishing_photos')
                ->where('fishing_id', (int) $id)
                ->orderBy('sort_order', 'ASC')
                ->orderBy('id', 'ASC')
                ->get()
                ->getResult();

            return $this->respondSuccess($row);
        } catch (\Exception $e) {
            log_message('error', 'FishingController show error: ' . $e->getMessage());
            return $this->respondError('조회 중 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 낚시터 수정
     * PUT /api/fishing/:id
     */
    public function update($id = null)
    {
        $auth = $this->requireAuth();
        if ($auth instanceof ResponseInterface) {
            return $auth;
        }

        if (empty($id)) {
            return $this->respondError('ID가 필요합니다.', ResponseInterface::HTTP_BAD_REQUEST);
        }

        try {
            $payload = $this->request->getJSON(true);
            if (!is_array($payload) || empty($payload)) {
                $payload = $this->request->getRawInput() ?? [];
            }

            $fieldId = (int) ($payload['field_id'] ?? 0);
            $areaId  = (int) ($payload['area_id'] ?? 0);
            $name    = trim((string) ($payload['name'] ?? ''));

            if ($fieldId <= 0) return $this->respondError('분야를 선택하세요.', ResponseInterface::HTTP_BAD_REQUEST);
            if ($areaId <= 0)  return $this->respondError('지역을 선택하세요.', ResponseInterface::HTTP_BAD_REQUEST);
            if ($name === '')  return $this->respondError('낚시터명을 입력하세요.', ResponseInterface::HTTP_BAD_REQUEST);
            if (mb_strlen($name) > 100) return $this->respondError('낚시터명은 100자 이내로 입력하세요.', ResponseInterface::HTTP_BAD_REQUEST);

            $db = $this->getDB();

            $exists = $db->table($this->table)
                ->where('id', (int) $id)->where('deleted_YN', 'N')->countAllResults();
            if ($exists === 0) {
                return $this->respondError('해당 낚시터를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
            }

            $fieldExists = $db->table('fishing_field')
                ->where('id', $fieldId)->where('deleted_YN', 'N')->countAllResults();
            if ($fieldExists === 0) return $this->respondError('존재하지 않는 분야입니다.', ResponseInterface::HTTP_BAD_REQUEST);

            $areaExists = $db->table('fishing_area')
                ->where('id', $areaId)->where('deleted_YN', 'N')->countAllResults();
            if ($areaExists === 0) return $this->respondError('존재하지 않는 지역입니다.', ResponseInterface::HTTP_BAD_REQUEST);

            $partnership = (($payload['partnership_YN'] ?? 'N') === 'Y') ? 'Y' : 'N';
            $status      = (($payload['status_YN'] ?? 'Y') === 'N') ? 'N' : 'Y';

            $updateData = [
                'field_id'        => $fieldId,
                'area_id'         => $areaId,
                'name'            => $name,
                'operating_hours' => trim((string) ($payload['operating_hours'] ?? '')),
                'fish_species'    => trim((string) ($payload['fish_species'] ?? '')),
                'zip_code'        => trim((string) ($payload['zip_code'] ?? '')),
                'address'         => trim((string) ($payload['address'] ?? '')),
                'address_detail'  => trim((string) ($payload['address_detail'] ?? '')),
                'address_refer'   => trim((string) ($payload['address_refer'] ?? '')),
                'lat'             => trim((string) ($payload['lat'] ?? '')),
                'lng'             => trim((string) ($payload['lng'] ?? '')),
                'partnership_YN'  => $partnership,
                'status_YN'       => $status,
                'updated_at'      => date('Y-m-d H:i:s'),
            ];

            if ($partnership === 'Y') {
                $updateData['bank_code']      = trim((string) ($payload['bank_code'] ?? ''));
                $updateData['account_number'] = $this->encryptValue(trim((string) ($payload['account_number'] ?? '')));
                $updateData['account_holder'] = trim((string) ($payload['account_holder'] ?? ''));
            } else {
                $updateData['bank_code']      = '';
                $updateData['account_number'] = '';
                $updateData['account_holder'] = '';
            }

            $db->table($this->table)->where('id', (int) $id)->update($updateData);

            $row = $db->table($this->table)->where('id', (int) $id)->get()->getRow();
            if ($row) $row->account_number = $this->decryptValue($row->account_number);

            return $this->respondSuccess($row, '낚시터가 수정되었습니다.');
        } catch (\Exception $e) {
            log_message('error', 'FishingController update error: ' . $e->getMessage());
            return $this->respondError('수정 중 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 낚시터 삭제 (soft delete)
     * DELETE /api/fishing/:id
     */
    public function delete($id = null)
    {
        $auth = $this->requireAuth();
        if ($auth instanceof ResponseInterface) {
            return $auth;
        }

        if (empty($id)) {
            return $this->respondError('ID가 필요합니다.', ResponseInterface::HTTP_BAD_REQUEST);
        }

        try {
            $db = $this->getDB();
            $exists = $db->table($this->table)
                ->where('id', (int) $id)->where('deleted_YN', 'N')->countAllResults();
            if ($exists === 0) {
                return $this->respondError('해당 낚시터를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
            }

            $db->table($this->table)
                ->where('id', (int) $id)
                ->update([
                    'deleted_YN' => 'Y',
                    'updated_at' => date('Y-m-d H:i:s'),
                ]);

            return $this->respondSuccess(null, '낚시터가 삭제되었습니다.');
        } catch (\Exception $e) {
            log_message('error', 'FishingController delete error: ' . $e->getMessage());
            return $this->respondError('삭제 중 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 낚시터 사진 삭제 (파일 + DB hard delete)
     * DELETE /api/fishing/photo/:photoId
     */
    public function deletePhoto($photoId = null)
    {
        $auth = $this->requireAuth();
        if ($auth instanceof ResponseInterface) {
            return $auth;
        }

        if (empty($photoId)) {
            return $this->respondError('사진 ID가 필요합니다.', ResponseInterface::HTTP_BAD_REQUEST);
        }

        try {
            $db = $this->getDB();
            $photo = $db->table('fishing_photos')->where('id', (int) $photoId)->get()->getRow();
            if (!$photo) {
                return $this->respondError('해당 사진을 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
            }

            $fullPath = FCPATH . ltrim($photo->file_path, '/');
            if (is_file($fullPath)) {
                @unlink($fullPath);
            }

            $db->table('fishing_photos')->where('id', (int) $photoId)->delete();

            return $this->respondSuccess(null, '사진이 삭제되었습니다.');
        } catch (\Exception $e) {
            log_message('error', 'FishingController deletePhoto error: ' . $e->getMessage());
            return $this->respondError('사진 삭제 중 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 낚시터 사진 업로드 (다중)
     * POST /api/fishing/:id/photos   (multipart, photos[])
     */
    public function uploadPhotos($id = null)
    {
        $auth = $this->requireAuth();
        if ($auth instanceof ResponseInterface) {
            return $auth;
        }

        if (empty($id)) {
            return $this->respondError('낚시터 ID가 필요합니다.', ResponseInterface::HTTP_BAD_REQUEST);
        }

        try {
            $db = $this->getDB();

            $exists = $db->table($this->table)
                ->where('id', (int) $id)->where('deleted_YN', 'N')->countAllResults();
            if ($exists === 0) {
                return $this->respondError('해당 낚시터를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
            }

            $files = $this->request->getFileMultiple('photos');
            if (empty($files)) {
                return $this->respondError('업로드할 사진이 없습니다.', ResponseInterface::HTTP_BAD_REQUEST);
            }

            $allowed = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
            $uploadPath = FCPATH . 'uploads/fishing/';
            if (!is_dir($uploadPath)) {
                mkdir($uploadPath, 0755, true);
            }

            $maxRow = $db->table('fishing_photos')
                ->selectMax('sort_order')
                ->where('fishing_id', (int) $id)
                ->get()->getRow();
            $order = $maxRow && $maxRow->sort_order !== null ? (int) $maxRow->sort_order + 1 : 0;

            $saved = [];
            foreach ($files as $file) {
                if (!$file->isValid()) continue;
                $mime = $file->getMimeType();
                if (!in_array($mime, $allowed, true)) continue;

                $originalName = $file->getClientName();
                $size = $file->getSize();
                $newName = $file->getRandomName();
                $file->move($uploadPath, $newName);
                $fullPath = $uploadPath . $newName;

                $width = null;
                $height = null;
                $info = @getimagesize($fullPath);
                if ($info) {
                    $width = $info[0];
                    $height = $info[1];
                }

                $photoData = [
                    'fishing_id'    => (int) $id,
                    'original_name' => $originalName,
                    'stored_name'   => $newName,
                    'file_path'     => '/uploads/fishing/' . $newName,
                    'file_size'     => $size,
                    'mime_type'     => $mime,
                    'width'         => $width,
                    'height'        => $height,
                    'sort_order'    => $order,
                    'created_at'    => date('Y-m-d H:i:s'),
                ];
                $db->table('fishing_photos')->insert($photoData);
                $photoData['id'] = $db->insertID();
                $saved[] = $photoData;
                $order++;
            }

            if (empty($saved)) {
                return $this->respondError('유효한 이미지 파일이 없습니다. (JPG/PNG/GIF/WebP만 허용)', ResponseInterface::HTTP_BAD_REQUEST);
            }

            return $this->respondSuccess($saved, count($saved) . '장의 사진이 업로드되었습니다.', ResponseInterface::HTTP_CREATED);
        } catch (\Exception $e) {
            log_message('error', 'FishingController uploadPhotos error: ' . $e->getMessage());
            return $this->respondError('사진 업로드 중 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
        }
    }

    /** 값 암호화 (빈 값은 그대로 빈 문자열) */
    private function encryptValue(string $plain): string
    {
        if ($plain === '') return '';
        $encrypter = \Config\Services::encrypter();
        return base64_encode($encrypter->encrypt($plain));
    }

    /** 값 복호화 (실패/빈 값이면 빈 문자열) */
    private function decryptValue(?string $cipher): string
    {
        if (empty($cipher)) return '';
        try {
            $encrypter = \Config\Services::encrypter();
            return $encrypter->decrypt(base64_decode($cipher));
        } catch (\Exception $e) {
            log_message('error', 'Account decrypt error: ' . $e->getMessage());
            return '';
        }
    }
}
