# ruff: noqa: I001
# Copyright 2026 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for the features list JSON feed handler."""

import testing_config  # isort: split
from typing import Optional

import flask
import settings
from api import featurelist_api as featurelist
from internals import core_enums, user_models
from internals.core_models import FeatureEntry

test_app = flask.Flask(
    __name__, template_folder=settings.get_flask_template_path()
)


class TestWithFeature(testing_config.CustomTestCase):
    """Base test class providing common feature setup."""

    REQUEST_PATH_FORMAT: Optional[str] = None
    HANDLER_CLASS: Optional[object] = None

    def setUp(self):
        """Sets up common user and feature entries for tests."""
        self.app_user = user_models.AppUser(email='registered@example.com')
        self.app_user.put()

        self.app_admin = user_models.AppUser(email='admin@example.com')
        self.app_admin.is_admin = True
        self.app_admin.put()

        self.fe_1 = FeatureEntry(
            name='feature one',
            summary='detailed sum',
            owner_emails=['owner@example.com'],
            category=1,
            intent_stage=core_enums.INTENT_IMPLEMENT,
        )
        self.fe_1.put()
        self.feature_id = self.fe_1.key.integer_id()

        self.request_path = (
            self.REQUEST_PATH_FORMAT % {'feature_id': self.feature_id}
            if self.REQUEST_PATH_FORMAT
            else ''
        )
        if self.HANDLER_CLASS:
            self.handler = self.HANDLER_CLASS()


class FeaturesJsonHandlerTest(TestWithFeature):
    """Tests for the FeaturesJsonHandler."""

    REQUEST_PATH_FORMAT = '/features.json'
    HANDLER_CLASS = featurelist.FeaturesJsonHandler

    def test_get_template_data(self):
        """User can get a JSON feed of all features."""
        testing_config.sign_in('user@example.com', 111)
        with test_app.test_request_context(self.request_path):
            json_data = self.handler.do_get()

        self.assertEqual(1, len(json_data))
        self.assertEqual('feature one', json_data[0]['name'])

    def test_get_template_data__unlisted_no_perms(self):
        """JSON feed does not include unlisted features for users who can't edit."""
        self.fe_1.unlisted = True
        self.fe_1.put()

        testing_config.sign_out()
        with test_app.test_request_context(self.request_path):
            json_data = self.handler.do_get()
        self.assertEqual(0, len(json_data))

        testing_config.sign_in('user@example.com', 111)
        with test_app.test_request_context(self.request_path):
            json_data = self.handler.do_get()
        self.assertEqual(0, len(json_data))

    def test_get_template_data__unlisted_can_edit(self):
        """JSON feed includes unlisted features for site editors and admins."""
        testing_config.sign_in('admin@example.com', 111)
        with test_app.test_request_context(self.request_path):
            json_data = self.handler.do_get()
        self.assertEqual(1, len(json_data))
        self.assertEqual('feature one', json_data[0]['name'])
